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/Instructions.h"
22 /// SimplifyBinOp - Given operands for a BinaryOperator, see if we can
23 /// fold the result. If not, this returns null.
24 Value *llvm::SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
25 const TargetData *TD) {
26 if (Constant *CLHS = dyn_cast<Constant>(LHS))
27 if (Constant *CRHS = dyn_cast<Constant>(RHS)) {
28 Constant *COps[] = {CLHS, CRHS};
29 return ConstantFoldInstOperands(Opcode, LHS->getType(), COps, 2, TD);
34 static const Type *GetCompareTy(Value *Op) {
35 return CmpInst::makeCmpResultType(Op->getType());
39 /// SimplifyICmpInst - Given operands for an ICmpInst, see if we can
40 /// fold the result. If not, this returns null.
41 Value *llvm::SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
42 const TargetData *TD) {
43 CmpInst::Predicate Pred = (CmpInst::Predicate)Predicate;
44 assert(CmpInst::isIntPredicate(Pred) && "Not an integer compare!");
46 if (Constant *CLHS = dyn_cast<Constant>(LHS))
47 if (Constant *CRHS = dyn_cast<Constant>(RHS))
48 return ConstantFoldCompareInstOperands(Pred, CLHS, CRHS, TD);
50 // ITy - This is the return type of the compare we're considering.
51 const Type *ITy = GetCompareTy(LHS);
53 // icmp X, X -> true/false
55 return ConstantInt::get(ITy, CmpInst::isTrueWhenEqual(Pred));
57 // If we have a constant, make sure it is on the RHS.
58 if (isa<Constant>(LHS)) {
60 Pred = CmpInst::getSwappedPredicate(Pred);
63 if (isa<UndefValue>(RHS)) // X icmp undef -> undef
64 return UndefValue::get(ITy);
66 // icmp <global/alloca*/null>, <global/alloca*/null> - Global/Stack value
67 // addresses never equal each other! We already know that Op0 != Op1.
68 if ((isa<GlobalValue>(LHS) || isa<AllocaInst>(LHS) ||
69 isa<ConstantPointerNull>(LHS)) &&
70 (isa<GlobalValue>(RHS) || isa<AllocaInst>(RHS) ||
71 isa<ConstantPointerNull>(RHS)))
72 return ConstantInt::get(ITy, CmpInst::isFalseWhenEqual(Pred));
74 // See if we are doing a comparison with a constant.
75 if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) {
76 // If we have an icmp le or icmp ge instruction, turn it into the
77 // appropriate icmp lt or icmp gt instruction. This allows us to rely on
78 // them being folded in the code below.
81 case ICmpInst::ICMP_ULE:
82 if (CI->isMaxValue(false)) // A <=u MAX -> TRUE
83 return ConstantInt::getTrue(CI->getContext());
85 case ICmpInst::ICMP_SLE:
86 if (CI->isMaxValue(true)) // A <=s MAX -> TRUE
87 return ConstantInt::getTrue(CI->getContext());
89 case ICmpInst::ICMP_UGE:
90 if (CI->isMinValue(false)) // A >=u MIN -> TRUE
91 return ConstantInt::getTrue(CI->getContext());
93 case ICmpInst::ICMP_SGE:
94 if (CI->isMinValue(true)) // A >=s MIN -> TRUE
95 return ConstantInt::getTrue(CI->getContext());
106 /// SimplifyFCmpInst - Given operands for an FCmpInst, see if we can
107 /// fold the result. If not, this returns null.
108 Value *llvm::SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
109 const TargetData *TD) {
110 CmpInst::Predicate Pred = (CmpInst::Predicate)Predicate;
111 assert(CmpInst::isFPPredicate(Pred) && "Not an FP compare!");
113 if (Constant *CLHS = dyn_cast<Constant>(LHS))
114 if (Constant *CRHS = dyn_cast<Constant>(RHS))
115 return ConstantFoldCompareInstOperands(Pred, CLHS, CRHS, TD);
117 // Fold trivial predicates.
118 if (Pred == FCmpInst::FCMP_FALSE)
119 return ConstantInt::get(GetCompareTy(LHS), 0);
120 if (Pred == FCmpInst::FCMP_TRUE)
121 return ConstantInt::get(GetCompareTy(LHS), 1);
123 // If we have a constant, make sure it is on the RHS.
124 if (isa<Constant>(LHS)) {
126 Pred = CmpInst::getSwappedPredicate(Pred);
129 if (isa<UndefValue>(RHS)) // fcmp pred X, undef -> undef
130 return UndefValue::get(GetCompareTy(LHS));
132 // fcmp x,x -> true/false. Not all compares are foldable.
134 if (CmpInst::isTrueWhenEqual(Pred))
135 return ConstantInt::get(GetCompareTy(LHS), 1);
136 if (CmpInst::isFalseWhenEqual(Pred))
137 return ConstantInt::get(GetCompareTy(LHS), 0);
140 // Handle fcmp with constant RHS
141 if (Constant *RHSC = dyn_cast<Constant>(RHS)) {
142 // If the constant is a nan, see if we can fold the comparison based on it.
143 if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHSC)) {
144 if (CFP->getValueAPF().isNaN()) {
145 if (FCmpInst::isOrdered(Pred)) // True "if ordered and foo"
146 return ConstantInt::getFalse(CFP->getContext());
147 assert(FCmpInst::isUnordered(Pred) &&
148 "Comparison must be either ordered or unordered!");
149 // True if unordered.
150 return ConstantInt::getTrue(CFP->getContext());
160 /// SimplifyCmpInst - Given operands for a CmpInst, see if we can
162 Value *llvm::SimplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
163 const TargetData *TD) {
164 if (CmpInst::isIntPredicate((CmpInst::Predicate)Predicate))
165 return SimplifyICmpInst(Predicate, LHS, RHS, TD);
166 return SimplifyFCmpInst(Predicate, LHS, RHS, TD);