1 //===- InstructionSimplify.cpp - Fold instruction operands ----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements routines for folding instructions into simpler forms
11 // that do not require creating new instructions. For example, this does
12 // constant folding, and can handle identities like (X&0)->0.
14 //===----------------------------------------------------------------------===//
16 #include "llvm/Analysis/InstructionSimplify.h"
17 #include "llvm/Analysis/ConstantFolding.h"
18 #include "llvm/Support/ValueHandle.h"
19 #include "llvm/Instructions.h"
20 #include "llvm/Support/PatternMatch.h"
22 using namespace llvm::PatternMatch;
24 /// SimplifyAddInst - Given operands for an Add, see if we can
25 /// fold the result. If not, this returns null.
26 Value *llvm::SimplifyAddInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW,
27 const TargetData *TD) {
28 if (Constant *CLHS = dyn_cast<Constant>(Op0)) {
29 if (Constant *CRHS = dyn_cast<Constant>(Op1)) {
30 Constant *Ops[] = { CLHS, CRHS };
31 return ConstantFoldInstOperands(Instruction::Add, CLHS->getType(),
35 // Canonicalize the constant to the RHS.
39 if (Constant *Op1C = dyn_cast<Constant>(Op1)) {
41 if (isa<UndefValue>(Op1C))
45 if (Op1C->isNullValue())
49 // FIXME: Could pull several more out of instcombine.
53 /// SimplifyAndInst - Given operands for an And, see if we can
54 /// fold the result. If not, this returns null.
55 Value *llvm::SimplifyAndInst(Value *Op0, Value *Op1, const TargetData *TD) {
56 if (Constant *CLHS = dyn_cast<Constant>(Op0)) {
57 if (Constant *CRHS = dyn_cast<Constant>(Op1)) {
58 Constant *Ops[] = { CLHS, CRHS };
59 return ConstantFoldInstOperands(Instruction::And, CLHS->getType(),
63 // Canonicalize the constant to the RHS.
68 if (isa<UndefValue>(Op1))
69 return Constant::getNullValue(Op0->getType());
76 if (isa<ConstantAggregateZero>(Op1))
80 if (ConstantVector *CP = dyn_cast<ConstantVector>(Op1))
81 if (CP->isAllOnesValue())
84 if (ConstantInt *Op1CI = dyn_cast<ConstantInt>(Op1)) {
89 if (Op1CI->isAllOnesValue())
93 // A & ~A = ~A & A = 0
95 if ((match(Op0, m_Not(m_Value(A))) && A == Op1) ||
96 (match(Op1, m_Not(m_Value(A))) && A == Op0))
97 return Constant::getNullValue(Op0->getType());
100 if (match(Op0, m_Or(m_Value(A), m_Value(B))) &&
101 (A == Op1 || B == Op1))
105 if (match(Op1, m_Or(m_Value(A), m_Value(B))) &&
106 (A == Op0 || B == Op0))
109 // (A & B) & A -> A & B
110 if (match(Op0, m_And(m_Value(A), m_Value(B))) &&
111 (A == Op1 || B == Op1))
114 // A & (A & B) -> A & B
115 if (match(Op1, m_And(m_Value(A), m_Value(B))) &&
116 (A == Op0 || B == Op0))
122 /// SimplifyOrInst - Given operands for an Or, see if we can
123 /// fold the result. If not, this returns null.
124 Value *llvm::SimplifyOrInst(Value *Op0, Value *Op1, const TargetData *TD) {
125 if (Constant *CLHS = dyn_cast<Constant>(Op0)) {
126 if (Constant *CRHS = dyn_cast<Constant>(Op1)) {
127 Constant *Ops[] = { CLHS, CRHS };
128 return ConstantFoldInstOperands(Instruction::Or, CLHS->getType(),
132 // Canonicalize the constant to the RHS.
137 if (isa<UndefValue>(Op1))
138 return Constant::getAllOnesValue(Op0->getType());
145 if (isa<ConstantAggregateZero>(Op1))
148 // X | <-1,-1> = <-1,-1>
149 if (ConstantVector *CP = dyn_cast<ConstantVector>(Op1))
150 if (CP->isAllOnesValue())
153 if (ConstantInt *Op1CI = dyn_cast<ConstantInt>(Op1)) {
158 if (Op1CI->isAllOnesValue())
162 // A | ~A = ~A | A = -1
164 if ((match(Op0, m_Not(m_Value(A))) && A == Op1) ||
165 (match(Op1, m_Not(m_Value(A))) && A == Op0))
166 return Constant::getAllOnesValue(Op0->getType());
169 if (match(Op0, m_And(m_Value(A), m_Value(B))) &&
170 (A == Op1 || B == Op1))
174 if (match(Op1, m_And(m_Value(A), m_Value(B))) &&
175 (A == Op0 || B == Op0))
178 // (A | B) | A -> A | B
179 if (match(Op0, m_Or(m_Value(A), m_Value(B))) &&
180 (A == Op1 || B == Op1))
183 // A | (A | B) -> A | B
184 if (match(Op1, m_Or(m_Value(A), m_Value(B))) &&
185 (A == Op0 || B == Op0))
192 static const Type *GetCompareTy(Value *Op) {
193 return CmpInst::makeCmpResultType(Op->getType());
197 /// SimplifyICmpInst - Given operands for an ICmpInst, see if we can
198 /// fold the result. If not, this returns null.
199 Value *llvm::SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
200 const TargetData *TD) {
201 CmpInst::Predicate Pred = (CmpInst::Predicate)Predicate;
202 assert(CmpInst::isIntPredicate(Pred) && "Not an integer compare!");
204 if (Constant *CLHS = dyn_cast<Constant>(LHS)) {
205 if (Constant *CRHS = dyn_cast<Constant>(RHS))
206 return ConstantFoldCompareInstOperands(Pred, CLHS, CRHS, TD);
208 // If we have a constant, make sure it is on the RHS.
210 Pred = CmpInst::getSwappedPredicate(Pred);
213 // ITy - This is the return type of the compare we're considering.
214 const Type *ITy = GetCompareTy(LHS);
216 // icmp X, X -> true/false
217 // X icmp undef -> true/false. For example, icmp ugt %X, undef -> false
218 // because X could be 0.
219 if (LHS == RHS || isa<UndefValue>(RHS))
220 return ConstantInt::get(ITy, CmpInst::isTrueWhenEqual(Pred));
222 // icmp <global/alloca*/null>, <global/alloca*/null> - Global/Stack value
223 // addresses never equal each other! We already know that Op0 != Op1.
224 if ((isa<GlobalValue>(LHS) || isa<AllocaInst>(LHS) ||
225 isa<ConstantPointerNull>(LHS)) &&
226 (isa<GlobalValue>(RHS) || isa<AllocaInst>(RHS) ||
227 isa<ConstantPointerNull>(RHS)))
228 return ConstantInt::get(ITy, CmpInst::isFalseWhenEqual(Pred));
230 // See if we are doing a comparison with a constant.
231 if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) {
232 // If we have an icmp le or icmp ge instruction, turn it into the
233 // appropriate icmp lt or icmp gt instruction. This allows us to rely on
234 // them being folded in the code below.
237 case ICmpInst::ICMP_ULE:
238 if (CI->isMaxValue(false)) // A <=u MAX -> TRUE
239 return ConstantInt::getTrue(CI->getContext());
241 case ICmpInst::ICMP_SLE:
242 if (CI->isMaxValue(true)) // A <=s MAX -> TRUE
243 return ConstantInt::getTrue(CI->getContext());
245 case ICmpInst::ICMP_UGE:
246 if (CI->isMinValue(false)) // A >=u MIN -> TRUE
247 return ConstantInt::getTrue(CI->getContext());
249 case ICmpInst::ICMP_SGE:
250 if (CI->isMinValue(true)) // A >=s MIN -> TRUE
251 return ConstantInt::getTrue(CI->getContext());
256 // If the comparison is with the result of a select instruction, check whether
257 // comparing with either branch of the select always yields the same value.
258 if (isa<SelectInst>(LHS) || isa<SelectInst>(RHS)) {
259 // Make sure the select is on the LHS.
260 if (!isa<SelectInst>(LHS)) {
262 Pred = CmpInst::getSwappedPredicate(Pred);
264 SelectInst *SI = cast<SelectInst>(LHS);
265 // Now that we have "icmp select(cond, TV, FV), RHS", analyse it.
266 // Does "icmp TV, RHS" simplify?
267 if (Value *TCmp = SimplifyICmpInst(Pred, SI->getTrueValue(), RHS, TD))
268 // It does! Does "icmp FV, RHS" simplify?
269 if (Value *FCmp = SimplifyICmpInst(Pred, SI->getFalseValue(), RHS, TD))
270 // It does! If they simplified to the same value, then use it as the
271 // result of the original comparison.
279 /// SimplifyFCmpInst - Given operands for an FCmpInst, see if we can
280 /// fold the result. If not, this returns null.
281 Value *llvm::SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
282 const TargetData *TD) {
283 CmpInst::Predicate Pred = (CmpInst::Predicate)Predicate;
284 assert(CmpInst::isFPPredicate(Pred) && "Not an FP compare!");
286 if (Constant *CLHS = dyn_cast<Constant>(LHS)) {
287 if (Constant *CRHS = dyn_cast<Constant>(RHS))
288 return ConstantFoldCompareInstOperands(Pred, CLHS, CRHS, TD);
290 // If we have a constant, make sure it is on the RHS.
292 Pred = CmpInst::getSwappedPredicate(Pred);
295 // Fold trivial predicates.
296 if (Pred == FCmpInst::FCMP_FALSE)
297 return ConstantInt::get(GetCompareTy(LHS), 0);
298 if (Pred == FCmpInst::FCMP_TRUE)
299 return ConstantInt::get(GetCompareTy(LHS), 1);
301 if (isa<UndefValue>(RHS)) // fcmp pred X, undef -> undef
302 return UndefValue::get(GetCompareTy(LHS));
304 // fcmp x,x -> true/false. Not all compares are foldable.
306 if (CmpInst::isTrueWhenEqual(Pred))
307 return ConstantInt::get(GetCompareTy(LHS), 1);
308 if (CmpInst::isFalseWhenEqual(Pred))
309 return ConstantInt::get(GetCompareTy(LHS), 0);
312 // Handle fcmp with constant RHS
313 if (Constant *RHSC = dyn_cast<Constant>(RHS)) {
314 // If the constant is a nan, see if we can fold the comparison based on it.
315 if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHSC)) {
316 if (CFP->getValueAPF().isNaN()) {
317 if (FCmpInst::isOrdered(Pred)) // True "if ordered and foo"
318 return ConstantInt::getFalse(CFP->getContext());
319 assert(FCmpInst::isUnordered(Pred) &&
320 "Comparison must be either ordered or unordered!");
321 // True if unordered.
322 return ConstantInt::getTrue(CFP->getContext());
324 // Check whether the constant is an infinity.
325 if (CFP->getValueAPF().isInfinity()) {
326 if (CFP->getValueAPF().isNegative()) {
328 case FCmpInst::FCMP_OLT:
329 // No value is ordered and less than negative infinity.
330 return ConstantInt::getFalse(CFP->getContext());
331 case FCmpInst::FCMP_UGE:
332 // All values are unordered with or at least negative infinity.
333 return ConstantInt::getTrue(CFP->getContext());
339 case FCmpInst::FCMP_OGT:
340 // No value is ordered and greater than infinity.
341 return ConstantInt::getFalse(CFP->getContext());
342 case FCmpInst::FCMP_ULE:
343 // All values are unordered with and at most infinity.
344 return ConstantInt::getTrue(CFP->getContext());
356 /// SimplifySelectInst - Given operands for a SelectInst, see if we can fold
357 /// the result. If not, this returns null.
358 Value *llvm::SimplifySelectInst(Value *CondVal, Value *TrueVal, Value *FalseVal,
359 const TargetData *TD) {
360 // select true, X, Y -> X
361 // select false, X, Y -> Y
362 if (ConstantInt *CB = dyn_cast<ConstantInt>(CondVal))
363 return CB->getZExtValue() ? TrueVal : FalseVal;
365 // select C, X, X -> X
366 if (TrueVal == FalseVal)
369 if (isa<UndefValue>(TrueVal)) // select C, undef, X -> X
371 if (isa<UndefValue>(FalseVal)) // select C, X, undef -> X
373 if (isa<UndefValue>(CondVal)) { // select undef, X, Y -> X or Y
374 if (isa<Constant>(TrueVal))
385 /// SimplifyGEPInst - Given operands for an GetElementPtrInst, see if we can
386 /// fold the result. If not, this returns null.
387 Value *llvm::SimplifyGEPInst(Value *const *Ops, unsigned NumOps,
388 const TargetData *TD) {
389 // getelementptr P -> P.
394 //if (isa<UndefValue>(Ops[0]))
395 // return UndefValue::get(GEP.getType());
397 // getelementptr P, 0 -> P.
399 if (ConstantInt *C = dyn_cast<ConstantInt>(Ops[1]))
403 // Check to see if this is constant foldable.
404 for (unsigned i = 0; i != NumOps; ++i)
405 if (!isa<Constant>(Ops[i]))
408 return ConstantExpr::getGetElementPtr(cast<Constant>(Ops[0]),
409 (Constant *const*)Ops+1, NumOps-1);
413 //=== Helper functions for higher up the class hierarchy.
415 /// SimplifyBinOp - Given operands for a BinaryOperator, see if we can
416 /// fold the result. If not, this returns null.
417 Value *llvm::SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
418 const TargetData *TD) {
420 case Instruction::And: return SimplifyAndInst(LHS, RHS, TD);
421 case Instruction::Or: return SimplifyOrInst(LHS, RHS, TD);
423 if (Constant *CLHS = dyn_cast<Constant>(LHS))
424 if (Constant *CRHS = dyn_cast<Constant>(RHS)) {
425 Constant *COps[] = {CLHS, CRHS};
426 return ConstantFoldInstOperands(Opcode, LHS->getType(), COps, 2, TD);
432 /// SimplifyCmpInst - Given operands for a CmpInst, see if we can
434 Value *llvm::SimplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
435 const TargetData *TD) {
436 if (CmpInst::isIntPredicate((CmpInst::Predicate)Predicate))
437 return SimplifyICmpInst(Predicate, LHS, RHS, TD);
438 return SimplifyFCmpInst(Predicate, LHS, RHS, TD);
442 /// SimplifyInstruction - See if we can compute a simplified version of this
443 /// instruction. If not, this returns null.
444 Value *llvm::SimplifyInstruction(Instruction *I, const TargetData *TD) {
445 switch (I->getOpcode()) {
447 return ConstantFoldInstruction(I, TD);
448 case Instruction::Add:
449 return SimplifyAddInst(I->getOperand(0), I->getOperand(1),
450 cast<BinaryOperator>(I)->hasNoSignedWrap(),
451 cast<BinaryOperator>(I)->hasNoUnsignedWrap(), TD);
452 case Instruction::And:
453 return SimplifyAndInst(I->getOperand(0), I->getOperand(1), TD);
454 case Instruction::Or:
455 return SimplifyOrInst(I->getOperand(0), I->getOperand(1), TD);
456 case Instruction::ICmp:
457 return SimplifyICmpInst(cast<ICmpInst>(I)->getPredicate(),
458 I->getOperand(0), I->getOperand(1), TD);
459 case Instruction::FCmp:
460 return SimplifyFCmpInst(cast<FCmpInst>(I)->getPredicate(),
461 I->getOperand(0), I->getOperand(1), TD);
462 case Instruction::Select:
463 return SimplifySelectInst(I->getOperand(0), I->getOperand(1),
464 I->getOperand(2), TD);
465 case Instruction::GetElementPtr: {
466 SmallVector<Value*, 8> Ops(I->op_begin(), I->op_end());
467 return SimplifyGEPInst(&Ops[0], Ops.size(), TD);
472 /// ReplaceAndSimplifyAllUses - Perform From->replaceAllUsesWith(To) and then
473 /// delete the From instruction. In addition to a basic RAUW, this does a
474 /// recursive simplification of the newly formed instructions. This catches
475 /// things where one simplification exposes other opportunities. This only
476 /// simplifies and deletes scalar operations, it does not change the CFG.
478 void llvm::ReplaceAndSimplifyAllUses(Instruction *From, Value *To,
479 const TargetData *TD) {
480 assert(From != To && "ReplaceAndSimplifyAllUses(X,X) is not valid!");
482 // FromHandle/ToHandle - This keeps a WeakVH on the from/to values so that
483 // we can know if it gets deleted out from under us or replaced in a
484 // recursive simplification.
485 WeakVH FromHandle(From);
488 while (!From->use_empty()) {
489 // Update the instruction to use the new value.
490 Use &TheUse = From->use_begin().getUse();
491 Instruction *User = cast<Instruction>(TheUse.getUser());
494 // Check to see if the instruction can be folded due to the operand
495 // replacement. For example changing (or X, Y) into (or X, -1) can replace
497 Value *SimplifiedVal;
499 // Sanity check to make sure 'User' doesn't dangle across
500 // SimplifyInstruction.
501 AssertingVH<> UserHandle(User);
503 SimplifiedVal = SimplifyInstruction(User, TD);
504 if (SimplifiedVal == 0) continue;
507 // Recursively simplify this user to the new value.
508 ReplaceAndSimplifyAllUses(User, SimplifiedVal, TD);
509 From = dyn_cast_or_null<Instruction>((Value*)FromHandle);
512 assert(ToHandle && "To value deleted by recursive simplification?");
514 // If the recursive simplification ended up revisiting and deleting
515 // 'From' then we're done.
520 // If 'From' has value handles referring to it, do a real RAUW to update them.
521 From->replaceAllUsesWith(To);
523 From->eraseFromParent();