blob: 507e6f2a854d4069cd675b5a391ed28b81d18e4a [file] [log] [blame]
Chris Lattnere6794492002-08-12 21:17:25 +00001//===- InstructionCombining.cpp - Combine multiple instructions -----------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattnerca081252001-12-14 16:52:21 +00009//
10// InstructionCombining - Combine instructions to form fewer, simple
Chris Lattner99f48c62002-09-02 04:59:56 +000011// instructions. This pass does not modify the CFG This pass is where algebraic
12// simplification happens.
Chris Lattnerca081252001-12-14 16:52:21 +000013//
14// This pass combines things like:
Chris Lattner07418422007-03-18 22:51:34 +000015// %Y = add i32 %X, 1
16// %Z = add i32 %Y, 1
Chris Lattnerca081252001-12-14 16:52:21 +000017// into:
Chris Lattner07418422007-03-18 22:51:34 +000018// %Z = add i32 %X, 2
Chris Lattnerca081252001-12-14 16:52:21 +000019//
20// This is a simple worklist driven algorithm.
21//
Chris Lattner216c7b82003-09-10 05:29:43 +000022// This pass guarantees that the following canonicalizations are performed on
Chris Lattnerbfb1d032003-07-23 21:41:57 +000023// the program:
24// 1. If a binary operator has a constant operand, it is moved to the RHS
Chris Lattnerdeaa0dd2003-08-12 21:53:41 +000025// 2. Bitwise operators with constant operands are always grouped so that
26// shifts are performed first, then or's, then and's, then xor's.
Reid Spencer266e42b2006-12-23 06:05:41 +000027// 3. Compare instructions are converted from <,>,<=,>= to ==,!= if possible
28// 4. All cmp instructions on boolean values are replaced with logical ops
Chris Lattnerede3fe02003-08-13 04:18:28 +000029// 5. add X, X is represented as (X*2) => (X << 1)
30// 6. Multiplies with a power-of-two constant argument are transformed into
31// shifts.
Chris Lattner7515cab2004-11-14 19:13:23 +000032// ... etc.
Chris Lattnerbfb1d032003-07-23 21:41:57 +000033//
Chris Lattnerca081252001-12-14 16:52:21 +000034//===----------------------------------------------------------------------===//
35
Chris Lattner7d2a5392004-03-13 23:54:27 +000036#define DEBUG_TYPE "instcombine"
Chris Lattnerb4cfa7f2002-05-07 20:03:00 +000037#include "llvm/Transforms/Scalar.h"
Chris Lattner00648e12004-10-12 04:52:52 +000038#include "llvm/IntrinsicInst.h"
Chris Lattner04805fa2002-02-26 21:46:54 +000039#include "llvm/Pass.h"
Chris Lattner1085bdf2002-11-04 16:18:53 +000040#include "llvm/DerivedTypes.h"
Chris Lattner0f1d8a32003-06-26 05:06:25 +000041#include "llvm/GlobalVariable.h"
Chris Lattner024f4ab2007-01-30 23:46:24 +000042#include "llvm/Analysis/ConstantFolding.h"
Chris Lattnerf4ad1652003-11-02 05:57:39 +000043#include "llvm/Target/TargetData.h"
44#include "llvm/Transforms/Utils/BasicBlockUtils.h"
45#include "llvm/Transforms/Utils/Local.h"
Chris Lattner69193f92004-04-05 01:30:19 +000046#include "llvm/Support/CallSite.h"
Nick Lewycky3b592142008-02-03 16:33:09 +000047#include "llvm/Support/ConstantRange.h"
Chris Lattner39c98bb2004-12-08 23:43:58 +000048#include "llvm/Support/Debug.h"
Chris Lattner69193f92004-04-05 01:30:19 +000049#include "llvm/Support/GetElementPtrTypeIterator.h"
Chris Lattner260ab202002-04-18 17:39:14 +000050#include "llvm/Support/InstVisitor.h"
Chris Lattner22d00a82005-08-02 19:16:58 +000051#include "llvm/Support/MathExtras.h"
Chris Lattnerd4252a72004-07-30 07:50:03 +000052#include "llvm/Support/PatternMatch.h"
Chris Lattner3d27be12006-08-27 12:54:02 +000053#include "llvm/Support/Compiler.h"
Chris Lattnerb15e2b12007-03-02 21:28:56 +000054#include "llvm/ADT/DenseMap.h"
Chris Lattnerf96f4a82007-01-31 04:40:53 +000055#include "llvm/ADT/SmallVector.h"
Chris Lattner7907e5f2007-02-15 19:41:52 +000056#include "llvm/ADT/SmallPtrSet.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000057#include "llvm/ADT/Statistic.h"
Chris Lattner39c98bb2004-12-08 23:43:58 +000058#include "llvm/ADT/STLExtras.h"
Chris Lattner053c0932002-05-14 15:24:07 +000059#include <algorithm>
Torok Edwinab207842008-04-20 08:33:11 +000060#include <climits>
Reid Spencer755d0e72007-03-26 17:44:01 +000061#include <sstream>
Chris Lattner8427bff2003-12-07 01:24:23 +000062using namespace llvm;
Chris Lattnerd4252a72004-07-30 07:50:03 +000063using namespace llvm::PatternMatch;
Brian Gaeke960707c2003-11-11 22:41:34 +000064
Chris Lattner79a42ac2006-12-19 21:40:18 +000065STATISTIC(NumCombined , "Number of insts combined");
66STATISTIC(NumConstProp, "Number of constant folds");
67STATISTIC(NumDeadInst , "Number of dead inst eliminated");
68STATISTIC(NumDeadStore, "Number of dead stores eliminated");
69STATISTIC(NumSunkInst , "Number of instructions sunk");
Chris Lattnerbf3a0992002-10-01 22:38:41 +000070
Chris Lattner79a42ac2006-12-19 21:40:18 +000071namespace {
Chris Lattner4a4c7fe2006-06-28 22:08:15 +000072 class VISIBILITY_HIDDEN InstCombiner
73 : public FunctionPass,
74 public InstVisitor<InstCombiner, Instruction*> {
Chris Lattner260ab202002-04-18 17:39:14 +000075 // Worklist of all of the instructions that need to be simplified.
Chris Lattnerb15e2b12007-03-02 21:28:56 +000076 std::vector<Instruction*> Worklist;
77 DenseMap<Instruction*, unsigned> WorklistMap;
Chris Lattnerf4ad1652003-11-02 05:57:39 +000078 TargetData *TD;
Chris Lattner8258b442007-03-04 04:27:24 +000079 bool MustPreserveLCSSA;
Chris Lattnerb15e2b12007-03-02 21:28:56 +000080 public:
Nick Lewyckye7da2d62007-05-06 13:37:16 +000081 static char ID; // Pass identification, replacement for typeid
Devang Patel09f162c2007-05-01 21:15:47 +000082 InstCombiner() : FunctionPass((intptr_t)&ID) {}
83
Chris Lattnerb15e2b12007-03-02 21:28:56 +000084 /// AddToWorkList - Add the specified instruction to the worklist if it
85 /// isn't already in it.
86 void AddToWorkList(Instruction *I) {
87 if (WorklistMap.insert(std::make_pair(I, Worklist.size())))
88 Worklist.push_back(I);
89 }
90
91 // RemoveFromWorkList - remove I from the worklist if it exists.
92 void RemoveFromWorkList(Instruction *I) {
93 DenseMap<Instruction*, unsigned>::iterator It = WorklistMap.find(I);
94 if (It == WorklistMap.end()) return; // Not in worklist.
95
96 // Don't bother moving everything down, just null out the slot.
97 Worklist[It->second] = 0;
98
99 WorklistMap.erase(It);
100 }
101
102 Instruction *RemoveOneFromWorkList() {
103 Instruction *I = Worklist.back();
104 Worklist.pop_back();
105 WorklistMap.erase(I);
106 return I;
107 }
Chris Lattner260ab202002-04-18 17:39:14 +0000108
Chris Lattnerb15e2b12007-03-02 21:28:56 +0000109
Chris Lattner51ea1272004-02-28 05:22:00 +0000110 /// AddUsersToWorkList - When an instruction is simplified, add all users of
111 /// the instruction to the work lists because they might get more simplified
112 /// now.
113 ///
Chris Lattner2590e512006-02-07 06:56:34 +0000114 void AddUsersToWorkList(Value &I) {
Chris Lattner113f4f42002-06-25 16:13:24 +0000115 for (Value::use_iterator UI = I.use_begin(), UE = I.use_end();
Chris Lattner260ab202002-04-18 17:39:14 +0000116 UI != UE; ++UI)
Chris Lattnerb15e2b12007-03-02 21:28:56 +0000117 AddToWorkList(cast<Instruction>(*UI));
Chris Lattner260ab202002-04-18 17:39:14 +0000118 }
119
Chris Lattner51ea1272004-02-28 05:22:00 +0000120 /// AddUsesToWorkList - When an instruction is simplified, add operands to
121 /// the work lists because they might get more simplified now.
122 ///
123 void AddUsesToWorkList(Instruction &I) {
124 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
125 if (Instruction *Op = dyn_cast<Instruction>(I.getOperand(i)))
Chris Lattnerb15e2b12007-03-02 21:28:56 +0000126 AddToWorkList(Op);
Chris Lattner51ea1272004-02-28 05:22:00 +0000127 }
Chris Lattner2deeaea2006-10-05 06:55:50 +0000128
129 /// AddSoonDeadInstToWorklist - The specified instruction is about to become
130 /// dead. Add all of its operands to the worklist, turning them into
131 /// undef's to reduce the number of uses of those instructions.
132 ///
133 /// Return the specified operand before it is turned into an undef.
134 ///
135 Value *AddSoonDeadInstToWorklist(Instruction &I, unsigned op) {
136 Value *R = I.getOperand(op);
137
138 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
139 if (Instruction *Op = dyn_cast<Instruction>(I.getOperand(i))) {
Chris Lattnerb15e2b12007-03-02 21:28:56 +0000140 AddToWorkList(Op);
Chris Lattner2deeaea2006-10-05 06:55:50 +0000141 // Set the operand to undef to drop the use.
142 I.setOperand(i, UndefValue::get(Op->getType()));
143 }
144
145 return R;
146 }
Chris Lattner51ea1272004-02-28 05:22:00 +0000147
Chris Lattner260ab202002-04-18 17:39:14 +0000148 public:
Chris Lattner113f4f42002-06-25 16:13:24 +0000149 virtual bool runOnFunction(Function &F);
Chris Lattner960a5432007-03-03 02:04:50 +0000150
151 bool DoOneIteration(Function &F, unsigned ItNum);
Chris Lattner260ab202002-04-18 17:39:14 +0000152
Chris Lattnerf12cc842002-04-28 21:27:06 +0000153 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnerf4ad1652003-11-02 05:57:39 +0000154 AU.addRequired<TargetData>();
Owen Andersona6968f82006-07-10 19:03:49 +0000155 AU.addPreservedID(LCSSAID);
Chris Lattner820d9712002-10-21 20:00:28 +0000156 AU.setPreservesCFG();
Chris Lattnerf12cc842002-04-28 21:27:06 +0000157 }
158
Chris Lattner69193f92004-04-05 01:30:19 +0000159 TargetData &getTargetData() const { return *TD; }
160
Chris Lattner260ab202002-04-18 17:39:14 +0000161 // Visitation implementation - Implement instruction combining for different
162 // instruction types. The semantics are as follows:
163 // Return Value:
164 // null - No change was made
Chris Lattnere6794492002-08-12 21:17:25 +0000165 // I - Change was made, I is still valid, I may be dead though
Chris Lattner260ab202002-04-18 17:39:14 +0000166 // otherwise - Change was made, replace I with returned instruction
Misha Brukmanb1c93172005-04-21 23:48:37 +0000167 //
Chris Lattner113f4f42002-06-25 16:13:24 +0000168 Instruction *visitAdd(BinaryOperator &I);
169 Instruction *visitSub(BinaryOperator &I);
170 Instruction *visitMul(BinaryOperator &I);
Reid Spencer7eb55b32006-11-02 01:53:59 +0000171 Instruction *visitURem(BinaryOperator &I);
172 Instruction *visitSRem(BinaryOperator &I);
173 Instruction *visitFRem(BinaryOperator &I);
174 Instruction *commonRemTransforms(BinaryOperator &I);
175 Instruction *commonIRemTransforms(BinaryOperator &I);
Reid Spencer7e80b0b2006-10-26 06:15:43 +0000176 Instruction *commonDivTransforms(BinaryOperator &I);
177 Instruction *commonIDivTransforms(BinaryOperator &I);
178 Instruction *visitUDiv(BinaryOperator &I);
179 Instruction *visitSDiv(BinaryOperator &I);
180 Instruction *visitFDiv(BinaryOperator &I);
Chris Lattner113f4f42002-06-25 16:13:24 +0000181 Instruction *visitAnd(BinaryOperator &I);
182 Instruction *visitOr (BinaryOperator &I);
183 Instruction *visitXor(BinaryOperator &I);
Reid Spencer2341c222007-02-02 02:16:23 +0000184 Instruction *visitShl(BinaryOperator &I);
185 Instruction *visitAShr(BinaryOperator &I);
186 Instruction *visitLShr(BinaryOperator &I);
187 Instruction *commonShiftTransforms(BinaryOperator &I);
Reid Spencer266e42b2006-12-23 06:05:41 +0000188 Instruction *visitFCmpInst(FCmpInst &I);
189 Instruction *visitICmpInst(ICmpInst &I);
190 Instruction *visitICmpInstWithCastAndCast(ICmpInst &ICI);
Chris Lattnera74deaf2007-04-03 17:43:25 +0000191 Instruction *visitICmpInstWithInstAndIntCst(ICmpInst &ICI,
192 Instruction *LHS,
193 ConstantInt *RHS);
Chris Lattner3bbec592007-06-20 23:46:26 +0000194 Instruction *FoldICmpDivCst(ICmpInst &ICI, BinaryOperator *DivI,
195 ConstantInt *DivRHS);
Chris Lattnerd1f46d32005-04-24 06:59:08 +0000196
Reid Spencer266e42b2006-12-23 06:05:41 +0000197 Instruction *FoldGEPICmp(User *GEPLHS, Value *RHS,
198 ICmpInst::Predicate Cond, Instruction &I);
Reid Spencere0fc4df2006-10-20 07:07:24 +0000199 Instruction *FoldShiftByConstant(Value *Op0, ConstantInt *Op1,
Reid Spencer2341c222007-02-02 02:16:23 +0000200 BinaryOperator &I);
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000201 Instruction *commonCastTransforms(CastInst &CI);
202 Instruction *commonIntCastTransforms(CastInst &CI);
Chris Lattner1db224d2007-04-27 17:44:50 +0000203 Instruction *commonPointerCastTransforms(CastInst &CI);
Chris Lattner74ff60f2007-04-11 06:57:46 +0000204 Instruction *visitTrunc(TruncInst &CI);
205 Instruction *visitZExt(ZExtInst &CI);
206 Instruction *visitSExt(SExtInst &CI);
Chris Lattnerfa1e7ee2008-01-27 05:29:54 +0000207 Instruction *visitFPTrunc(FPTruncInst &CI);
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000208 Instruction *visitFPExt(CastInst &CI);
209 Instruction *visitFPToUI(CastInst &CI);
210 Instruction *visitFPToSI(CastInst &CI);
211 Instruction *visitUIToFP(CastInst &CI);
212 Instruction *visitSIToFP(CastInst &CI);
213 Instruction *visitPtrToInt(CastInst &CI);
Chris Lattner2940c5c2008-01-08 07:23:51 +0000214 Instruction *visitIntToPtr(IntToPtrInst &CI);
Chris Lattner1db224d2007-04-27 17:44:50 +0000215 Instruction *visitBitCast(BitCastInst &CI);
Chris Lattner411336f2005-01-19 21:50:18 +0000216 Instruction *FoldSelectOpOp(SelectInst &SI, Instruction *TI,
217 Instruction *FI);
Chris Lattnerb909e8b2004-03-12 05:52:32 +0000218 Instruction *visitSelectInst(SelectInst &CI);
Chris Lattner970c33a2003-06-19 17:00:31 +0000219 Instruction *visitCallInst(CallInst &CI);
220 Instruction *visitInvokeInst(InvokeInst &II);
Chris Lattner113f4f42002-06-25 16:13:24 +0000221 Instruction *visitPHINode(PHINode &PN);
222 Instruction *visitGetElementPtrInst(GetElementPtrInst &GEP);
Chris Lattner1085bdf2002-11-04 16:18:53 +0000223 Instruction *visitAllocationInst(AllocationInst &AI);
Chris Lattner8427bff2003-12-07 01:24:23 +0000224 Instruction *visitFreeInst(FreeInst &FI);
Chris Lattner0f1d8a32003-06-26 05:06:25 +0000225 Instruction *visitLoadInst(LoadInst &LI);
Chris Lattner31f486c2005-01-31 05:36:43 +0000226 Instruction *visitStoreInst(StoreInst &SI);
Chris Lattner9eef8a72003-06-04 04:46:00 +0000227 Instruction *visitBranchInst(BranchInst &BI);
Chris Lattner4c9c20a2004-07-03 00:26:11 +0000228 Instruction *visitSwitchInst(SwitchInst &SI);
Chris Lattner39fac442006-04-15 01:39:45 +0000229 Instruction *visitInsertElementInst(InsertElementInst &IE);
Robert Bocchinoa8352962006-01-13 22:48:06 +0000230 Instruction *visitExtractElementInst(ExtractElementInst &EI);
Chris Lattnerfbb77a42006-04-10 22:45:52 +0000231 Instruction *visitShuffleVectorInst(ShuffleVectorInst &SVI);
Chris Lattner260ab202002-04-18 17:39:14 +0000232
233 // visitInstruction - Specify what to return for unhandled instructions...
Chris Lattner113f4f42002-06-25 16:13:24 +0000234 Instruction *visitInstruction(Instruction &I) { return 0; }
Chris Lattner6d14f2a2002-08-09 23:47:40 +0000235
Chris Lattner970c33a2003-06-19 17:00:31 +0000236 private:
Chris Lattneraec3d942003-10-07 22:32:43 +0000237 Instruction *visitCallSite(CallSite CS);
Chris Lattner970c33a2003-06-19 17:00:31 +0000238 bool transformConstExprCastCall(CallSite CS);
Duncan Sands6d5da712007-09-17 10:26:40 +0000239 Instruction *transformCallThroughTrampoline(CallSite CS);
Evan Chengc3cf9f82008-03-24 00:21:34 +0000240 Instruction *transformZExtICmp(ICmpInst *ICI, Instruction &CI,
241 bool DoXform = true);
Chris Lattner970c33a2003-06-19 17:00:31 +0000242
Chris Lattner69193f92004-04-05 01:30:19 +0000243 public:
Chris Lattner6d14f2a2002-08-09 23:47:40 +0000244 // InsertNewInstBefore - insert an instruction New before instruction Old
245 // in the program. Add the new instruction to the worklist.
246 //
Chris Lattner623826c2004-09-28 21:48:02 +0000247 Instruction *InsertNewInstBefore(Instruction *New, Instruction &Old) {
Chris Lattner65217ff2002-08-23 18:32:43 +0000248 assert(New && New->getParent() == 0 &&
249 "New instruction already inserted into a basic block!");
Chris Lattner6d14f2a2002-08-09 23:47:40 +0000250 BasicBlock *BB = Old.getParent();
251 BB->getInstList().insert(&Old, New); // Insert inst
Chris Lattnerb15e2b12007-03-02 21:28:56 +0000252 AddToWorkList(New);
Chris Lattnere79e8542004-02-23 06:38:22 +0000253 return New;
Chris Lattner6d14f2a2002-08-09 23:47:40 +0000254 }
255
Chris Lattner7e794272004-09-24 15:21:34 +0000256 /// InsertCastBefore - Insert a cast of V to TY before the instruction POS.
257 /// This also adds the cast to the worklist. Finally, this returns the
258 /// cast.
Reid Spencer13bc5d72006-12-12 09:18:51 +0000259 Value *InsertCastBefore(Instruction::CastOps opc, Value *V, const Type *Ty,
260 Instruction &Pos) {
Chris Lattner7e794272004-09-24 15:21:34 +0000261 if (V->getType() == Ty) return V;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000262
Chris Lattnere79d2492006-04-06 19:19:17 +0000263 if (Constant *CV = dyn_cast<Constant>(V))
Reid Spencer13bc5d72006-12-12 09:18:51 +0000264 return ConstantExpr::getCast(opc, CV, Ty);
Chris Lattnere79d2492006-04-06 19:19:17 +0000265
Reid Spencer13bc5d72006-12-12 09:18:51 +0000266 Instruction *C = CastInst::create(opc, V, Ty, V->getName(), &Pos);
Chris Lattnerb15e2b12007-03-02 21:28:56 +0000267 AddToWorkList(C);
Chris Lattner7e794272004-09-24 15:21:34 +0000268 return C;
269 }
Chris Lattner5a866122008-01-13 22:23:22 +0000270
271 Value *InsertBitCastBefore(Value *V, const Type *Ty, Instruction &Pos) {
272 return InsertCastBefore(Instruction::BitCast, V, Ty, Pos);
273 }
274
Chris Lattner7e794272004-09-24 15:21:34 +0000275
Chris Lattner6d14f2a2002-08-09 23:47:40 +0000276 // ReplaceInstUsesWith - This method is to be used when an instruction is
277 // found to be dead, replacable with another preexisting expression. Here
278 // we add all uses of I to the worklist, replace all uses of I with the new
279 // value, then return I, so that the inst combiner will know that I was
280 // modified.
281 //
282 Instruction *ReplaceInstUsesWith(Instruction &I, Value *V) {
Chris Lattner51ea1272004-02-28 05:22:00 +0000283 AddUsersToWorkList(I); // Add all modified instrs to worklist
Chris Lattner8953b902004-04-05 02:10:19 +0000284 if (&I != V) {
285 I.replaceAllUsesWith(V);
286 return &I;
287 } else {
288 // If we are replacing the instruction with itself, this must be in a
289 // segment of unreachable code, so just clobber the instruction.
Chris Lattner8ba9ec92004-10-18 02:59:09 +0000290 I.replaceAllUsesWith(UndefValue::get(I.getType()));
Chris Lattner8953b902004-04-05 02:10:19 +0000291 return &I;
292 }
Chris Lattner6d14f2a2002-08-09 23:47:40 +0000293 }
Chris Lattner51ea1272004-02-28 05:22:00 +0000294
Chris Lattner2590e512006-02-07 06:56:34 +0000295 // UpdateValueUsesWith - This method is to be used when an value is
296 // found to be replacable with another preexisting expression or was
297 // updated. Here we add all uses of I to the worklist, replace all uses of
298 // I with the new value (unless the instruction was just updated), then
299 // return true, so that the inst combiner will know that I was modified.
300 //
301 bool UpdateValueUsesWith(Value *Old, Value *New) {
302 AddUsersToWorkList(*Old); // Add all modified instrs to worklist
303 if (Old != New)
304 Old->replaceAllUsesWith(New);
305 if (Instruction *I = dyn_cast<Instruction>(Old))
Chris Lattnerb15e2b12007-03-02 21:28:56 +0000306 AddToWorkList(I);
Chris Lattner5b2edb12006-02-12 08:02:11 +0000307 if (Instruction *I = dyn_cast<Instruction>(New))
Chris Lattnerb15e2b12007-03-02 21:28:56 +0000308 AddToWorkList(I);
Chris Lattner2590e512006-02-07 06:56:34 +0000309 return true;
310 }
311
Chris Lattner51ea1272004-02-28 05:22:00 +0000312 // EraseInstFromFunction - When dealing with an instruction that has side
313 // effects or produces a void value, we can't rely on DCE to delete the
314 // instruction. Instead, visit methods should return the value returned by
315 // this function.
316 Instruction *EraseInstFromFunction(Instruction &I) {
317 assert(I.use_empty() && "Cannot erase instruction that is used!");
318 AddUsesToWorkList(I);
Chris Lattnerb15e2b12007-03-02 21:28:56 +0000319 RemoveFromWorkList(&I);
Chris Lattner95307542004-11-18 21:41:39 +0000320 I.eraseFromParent();
Chris Lattner51ea1272004-02-28 05:22:00 +0000321 return 0; // Don't do anything with FI
322 }
323
Chris Lattner3ac7c262003-08-13 20:16:26 +0000324 private:
Chris Lattnerdfae8be2003-07-24 17:35:25 +0000325 /// InsertOperandCastBefore - This inserts a cast of V to DestTy before the
326 /// InsertBefore instruction. This is specialized a bit to avoid inserting
327 /// casts that are known to not do anything...
328 ///
Reid Spencer13bc5d72006-12-12 09:18:51 +0000329 Value *InsertOperandCastBefore(Instruction::CastOps opcode,
330 Value *V, const Type *DestTy,
Chris Lattnerdfae8be2003-07-24 17:35:25 +0000331 Instruction *InsertBefore);
332
Reid Spencer266e42b2006-12-23 06:05:41 +0000333 /// SimplifyCommutative - This performs a few simplifications for
334 /// commutative operators.
Chris Lattner7fb29e12003-03-11 00:12:48 +0000335 bool SimplifyCommutative(BinaryOperator &I);
Chris Lattnerba1cb382003-09-19 17:17:26 +0000336
Reid Spencer266e42b2006-12-23 06:05:41 +0000337 /// SimplifyCompare - This reorders the operands of a CmpInst to get them in
338 /// most-complex to least-complex order.
339 bool SimplifyCompare(CmpInst &I);
340
Reid Spencer959a21d2007-03-23 21:24:59 +0000341 /// SimplifyDemandedBits - Attempts to replace V with a simpler value based
342 /// on the demanded bits.
Reid Spencer1791f232007-03-12 17:25:59 +0000343 bool SimplifyDemandedBits(Value *V, APInt DemandedMask,
344 APInt& KnownZero, APInt& KnownOne,
345 unsigned Depth = 0);
346
Chris Lattner2deeaea2006-10-05 06:55:50 +0000347 Value *SimplifyDemandedVectorElts(Value *V, uint64_t DemandedElts,
348 uint64_t &UndefElts, unsigned Depth = 0);
349
Chris Lattner6a4adcd2004-09-29 05:07:12 +0000350 // FoldOpIntoPhi - Given a binary operator or cast instruction which has a
351 // PHI node as operand #0, see if we can fold the instruction into the PHI
352 // (which is only possible if all operands to the PHI are constants).
353 Instruction *FoldOpIntoPhi(Instruction &I);
354
Chris Lattner7515cab2004-11-14 19:13:23 +0000355 // FoldPHIArgOpIntoPHI - If all operands to a PHI node are the same "unary"
356 // operator and they all are only used by the PHI, PHI together their
357 // inputs, and do the operation once, to the result of the PHI.
358 Instruction *FoldPHIArgOpIntoPHI(PHINode &PN);
Chris Lattnercadac0c2006-11-01 04:51:18 +0000359 Instruction *FoldPHIArgBinOpIntoPHI(PHINode &PN);
360
361
Zhou Sheng75b871f2007-01-11 12:24:14 +0000362 Instruction *OptAndOp(Instruction *Op, ConstantInt *OpRHS,
363 ConstantInt *AndRHS, BinaryOperator &TheAnd);
Chris Lattneraf517572005-09-18 04:24:45 +0000364
Zhou Sheng75b871f2007-01-11 12:24:14 +0000365 Value *FoldLogicalPlusAnd(Value *LHS, Value *RHS, ConstantInt *Mask,
Chris Lattneraf517572005-09-18 04:24:45 +0000366 bool isSub, Instruction &I);
Chris Lattner6862fbd2004-09-29 17:40:11 +0000367 Instruction *InsertRangeTest(Value *V, Constant *Lo, Constant *Hi,
Reid Spencer266e42b2006-12-23 06:05:41 +0000368 bool isSigned, bool Inside, Instruction &IB);
Chris Lattner1db224d2007-04-27 17:44:50 +0000369 Instruction *PromoteCastOfAllocation(BitCastInst &CI, AllocationInst &AI);
Chris Lattnerc482a9e2006-06-15 19:07:26 +0000370 Instruction *MatchBSwap(BinaryOperator &I);
Chris Lattner14a251b2007-04-15 00:07:55 +0000371 bool SimplifyStoreAtEndOfBlock(StoreInst &SI);
Chris Lattner57974c82008-01-13 23:50:23 +0000372 Instruction *SimplifyMemTransfer(MemIntrinsic *MI);
Chris Lattner2dc44262008-04-30 06:39:11 +0000373 Instruction *SimplifyMemSet(MemSetInst *MI);
Chris Lattner57974c82008-01-13 23:50:23 +0000374
Chris Lattnerc482a9e2006-06-15 19:07:26 +0000375
Reid Spencer74a528b2006-12-13 18:21:21 +0000376 Value *EvaluateInDifferentType(Value *V, const Type *Ty, bool isSigned);
Dan Gohman99b7b3f2008-04-10 18:43:06 +0000377
378 void ComputeMaskedBits(Value *V, const APInt &Mask, APInt& KnownZero,
379 APInt& KnownOne, unsigned Depth = 0);
380 bool MaskedValueIsZero(Value *V, const APInt& Mask, unsigned Depth = 0);
381 bool CanEvaluateInDifferentType(Value *V, const IntegerType *Ty,
382 unsigned CastOpc,
383 int &NumCastsRemoved);
384 unsigned GetOrEnforceKnownAlignment(Value *V,
385 unsigned PrefAlign = 0);
Chris Lattner260ab202002-04-18 17:39:14 +0000386 };
Chris Lattnerb28b6802002-07-23 18:06:35 +0000387
Devang Patel8c78a0b2007-05-03 01:11:54 +0000388 char InstCombiner::ID = 0;
Chris Lattnerc2d3d312006-08-27 22:42:52 +0000389 RegisterPass<InstCombiner> X("instcombine", "Combine redundant instructions");
Chris Lattner260ab202002-04-18 17:39:14 +0000390}
391
Chris Lattnerdcf240a2003-03-10 21:43:22 +0000392// getComplexity: Assign a complexity or rank value to LLVM Values...
Chris Lattner81a7a232004-10-16 18:11:37 +0000393// 0 -> undef, 1 -> Const, 2 -> Other, 3 -> Arg, 3 -> Unary, 4 -> OtherInst
Chris Lattnerdcf240a2003-03-10 21:43:22 +0000394static unsigned getComplexity(Value *V) {
395 if (isa<Instruction>(V)) {
396 if (BinaryOperator::isNeg(V) || BinaryOperator::isNot(V))
Chris Lattner81a7a232004-10-16 18:11:37 +0000397 return 3;
398 return 4;
Chris Lattnerdcf240a2003-03-10 21:43:22 +0000399 }
Chris Lattner81a7a232004-10-16 18:11:37 +0000400 if (isa<Argument>(V)) return 3;
401 return isa<Constant>(V) ? (isa<UndefValue>(V) ? 0 : 1) : 2;
Chris Lattnerdcf240a2003-03-10 21:43:22 +0000402}
Chris Lattner260ab202002-04-18 17:39:14 +0000403
Chris Lattner7fb29e12003-03-11 00:12:48 +0000404// isOnlyUse - Return true if this instruction will be deleted if we stop using
405// it.
406static bool isOnlyUse(Value *V) {
Chris Lattnerf95d9b92003-10-15 16:48:29 +0000407 return V->hasOneUse() || isa<Constant>(V);
Chris Lattner7fb29e12003-03-11 00:12:48 +0000408}
409
Chris Lattnere79e8542004-02-23 06:38:22 +0000410// getPromotedType - Return the specified type promoted as it would be to pass
411// though a va_arg area...
412static const Type *getPromotedType(const Type *Ty) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000413 if (const IntegerType* ITy = dyn_cast<IntegerType>(Ty)) {
414 if (ITy->getBitWidth() < 32)
415 return Type::Int32Ty;
Chris Lattnerf79577d2007-05-23 01:17:04 +0000416 }
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000417 return Ty;
Chris Lattnere79e8542004-02-23 06:38:22 +0000418}
419
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000420/// getBitCastOperand - If the specified operand is a CastInst or a constant
421/// expression bitcast, return the operand value, otherwise return null.
422static Value *getBitCastOperand(Value *V) {
423 if (BitCastInst *I = dyn_cast<BitCastInst>(V))
Chris Lattner567b81f2005-09-13 00:40:14 +0000424 return I->getOperand(0);
425 else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000426 if (CE->getOpcode() == Instruction::BitCast)
Chris Lattner567b81f2005-09-13 00:40:14 +0000427 return CE->getOperand(0);
428 return 0;
429}
430
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000431/// This function is a wrapper around CastInst::isEliminableCastPair. It
432/// simply extracts arguments and returns what that function returns.
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000433static Instruction::CastOps
434isEliminableCastPair(
435 const CastInst *CI, ///< The first cast instruction
436 unsigned opcode, ///< The opcode of the second cast instruction
437 const Type *DstTy, ///< The target type for the second cast instruction
438 TargetData *TD ///< The target data for pointer size
439) {
440
441 const Type *SrcTy = CI->getOperand(0)->getType(); // A from above
442 const Type *MidTy = CI->getType(); // B from above
Chris Lattner1d441ad2006-05-06 09:00:16 +0000443
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000444 // Get the opcodes of the two Cast instructions
445 Instruction::CastOps firstOp = Instruction::CastOps(CI->getOpcode());
446 Instruction::CastOps secondOp = Instruction::CastOps(opcode);
Chris Lattner1d441ad2006-05-06 09:00:16 +0000447
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000448 return Instruction::CastOps(
449 CastInst::isEliminableCastPair(firstOp, secondOp, SrcTy, MidTy,
450 DstTy, TD->getIntPtrType()));
Chris Lattner1d441ad2006-05-06 09:00:16 +0000451}
452
453/// ValueRequiresCast - Return true if the cast from "V to Ty" actually results
454/// in any code being generated. It does not require codegen if V is simple
455/// enough or if the cast can be folded into other casts.
Reid Spencer266e42b2006-12-23 06:05:41 +0000456static bool ValueRequiresCast(Instruction::CastOps opcode, const Value *V,
457 const Type *Ty, TargetData *TD) {
Chris Lattner1d441ad2006-05-06 09:00:16 +0000458 if (V->getType() == Ty || isa<Constant>(V)) return false;
459
Chris Lattner99155be2006-05-25 23:24:33 +0000460 // If this is another cast that can be eliminated, it isn't codegen either.
Chris Lattner1d441ad2006-05-06 09:00:16 +0000461 if (const CastInst *CI = dyn_cast<CastInst>(V))
Reid Spencer266e42b2006-12-23 06:05:41 +0000462 if (isEliminableCastPair(CI, opcode, Ty, TD))
Chris Lattner1d441ad2006-05-06 09:00:16 +0000463 return false;
464 return true;
465}
466
467/// InsertOperandCastBefore - This inserts a cast of V to DestTy before the
468/// InsertBefore instruction. This is specialized a bit to avoid inserting
469/// casts that are known to not do anything...
470///
Reid Spencer13bc5d72006-12-12 09:18:51 +0000471Value *InstCombiner::InsertOperandCastBefore(Instruction::CastOps opcode,
472 Value *V, const Type *DestTy,
Chris Lattner1d441ad2006-05-06 09:00:16 +0000473 Instruction *InsertBefore) {
474 if (V->getType() == DestTy) return V;
475 if (Constant *C = dyn_cast<Constant>(V))
Reid Spencer13bc5d72006-12-12 09:18:51 +0000476 return ConstantExpr::getCast(opcode, C, DestTy);
Chris Lattner1d441ad2006-05-06 09:00:16 +0000477
Reid Spencer13bc5d72006-12-12 09:18:51 +0000478 return InsertCastBefore(opcode, V, DestTy, *InsertBefore);
Chris Lattner1d441ad2006-05-06 09:00:16 +0000479}
480
Chris Lattnerdcf240a2003-03-10 21:43:22 +0000481// SimplifyCommutative - This performs a few simplifications for commutative
482// operators:
Chris Lattner260ab202002-04-18 17:39:14 +0000483//
Chris Lattnerdcf240a2003-03-10 21:43:22 +0000484// 1. Order operands such that they are listed from right (least complex) to
485// left (most complex). This puts constants before unary operators before
486// binary operators.
487//
Chris Lattner7fb29e12003-03-11 00:12:48 +0000488// 2. Transform: (op (op V, C1), C2) ==> (op V, (op C1, C2))
489// 3. Transform: (op (op V1, C1), (op V2, C2)) ==> (op (op V1, V2), (op C1,C2))
Chris Lattnerdcf240a2003-03-10 21:43:22 +0000490//
Chris Lattner7fb29e12003-03-11 00:12:48 +0000491bool InstCombiner::SimplifyCommutative(BinaryOperator &I) {
Chris Lattnerdcf240a2003-03-10 21:43:22 +0000492 bool Changed = false;
493 if (getComplexity(I.getOperand(0)) < getComplexity(I.getOperand(1)))
494 Changed = !I.swapOperands();
Misha Brukmanb1c93172005-04-21 23:48:37 +0000495
Chris Lattnerdcf240a2003-03-10 21:43:22 +0000496 if (!I.isAssociative()) return Changed;
497 Instruction::BinaryOps Opcode = I.getOpcode();
Chris Lattner7fb29e12003-03-11 00:12:48 +0000498 if (BinaryOperator *Op = dyn_cast<BinaryOperator>(I.getOperand(0)))
499 if (Op->getOpcode() == Opcode && isa<Constant>(Op->getOperand(1))) {
500 if (isa<Constant>(I.getOperand(1))) {
Chris Lattner34428442003-05-27 16:40:51 +0000501 Constant *Folded = ConstantExpr::get(I.getOpcode(),
502 cast<Constant>(I.getOperand(1)),
503 cast<Constant>(Op->getOperand(1)));
Chris Lattner7fb29e12003-03-11 00:12:48 +0000504 I.setOperand(0, Op->getOperand(0));
505 I.setOperand(1, Folded);
506 return true;
507 } else if (BinaryOperator *Op1=dyn_cast<BinaryOperator>(I.getOperand(1)))
508 if (Op1->getOpcode() == Opcode && isa<Constant>(Op1->getOperand(1)) &&
509 isOnlyUse(Op) && isOnlyUse(Op1)) {
510 Constant *C1 = cast<Constant>(Op->getOperand(1));
511 Constant *C2 = cast<Constant>(Op1->getOperand(1));
512
513 // Fold (op (op V1, C1), (op V2, C2)) ==> (op (op V1, V2), (op C1,C2))
Chris Lattner34428442003-05-27 16:40:51 +0000514 Constant *Folded = ConstantExpr::get(I.getOpcode(), C1, C2);
Chris Lattner7fb29e12003-03-11 00:12:48 +0000515 Instruction *New = BinaryOperator::create(Opcode, Op->getOperand(0),
516 Op1->getOperand(0),
517 Op1->getName(), &I);
Chris Lattnerb15e2b12007-03-02 21:28:56 +0000518 AddToWorkList(New);
Chris Lattner7fb29e12003-03-11 00:12:48 +0000519 I.setOperand(0, New);
520 I.setOperand(1, Folded);
521 return true;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000522 }
Chris Lattnerdcf240a2003-03-10 21:43:22 +0000523 }
Chris Lattnerdcf240a2003-03-10 21:43:22 +0000524 return Changed;
Chris Lattner260ab202002-04-18 17:39:14 +0000525}
Chris Lattnerca081252001-12-14 16:52:21 +0000526
Reid Spencer266e42b2006-12-23 06:05:41 +0000527/// SimplifyCompare - For a CmpInst this function just orders the operands
528/// so that theyare listed from right (least complex) to left (most complex).
529/// This puts constants before unary operators before binary operators.
530bool InstCombiner::SimplifyCompare(CmpInst &I) {
531 if (getComplexity(I.getOperand(0)) >= getComplexity(I.getOperand(1)))
532 return false;
533 I.swapOperands();
534 // Compare instructions are not associative so there's nothing else we can do.
535 return true;
536}
537
Chris Lattnerbb74e222003-03-10 23:06:50 +0000538// dyn_castNegVal - Given a 'sub' instruction, return the RHS of the instruction
539// if the LHS is a constant zero (which is the 'negate' form).
Chris Lattner9fa53de2002-05-06 16:49:18 +0000540//
Chris Lattnerbb74e222003-03-10 23:06:50 +0000541static inline Value *dyn_castNegVal(Value *V) {
542 if (BinaryOperator::isNeg(V))
Chris Lattnerd6f636a2005-04-24 07:30:14 +0000543 return BinaryOperator::getNegArgument(V);
Chris Lattnerbb74e222003-03-10 23:06:50 +0000544
Chris Lattner9ad0d552004-12-14 20:08:06 +0000545 // Constants can be considered to be negated values if they can be folded.
546 if (ConstantInt *C = dyn_cast<ConstantInt>(V))
547 return ConstantExpr::getNeg(C);
Chris Lattnerbb74e222003-03-10 23:06:50 +0000548 return 0;
Chris Lattner9fa53de2002-05-06 16:49:18 +0000549}
550
Chris Lattnerbb74e222003-03-10 23:06:50 +0000551static inline Value *dyn_castNotVal(Value *V) {
552 if (BinaryOperator::isNot(V))
Chris Lattnerd6f636a2005-04-24 07:30:14 +0000553 return BinaryOperator::getNotArgument(V);
Chris Lattnerbb74e222003-03-10 23:06:50 +0000554
555 // Constants can be considered to be not'ed values...
Zhou Sheng75b871f2007-01-11 12:24:14 +0000556 if (ConstantInt *C = dyn_cast<ConstantInt>(V))
Zhou Sheng9bc8ab12007-04-02 13:45:30 +0000557 return ConstantInt::get(~C->getValue());
Chris Lattnerbb74e222003-03-10 23:06:50 +0000558 return 0;
559}
560
Chris Lattner7fb29e12003-03-11 00:12:48 +0000561// dyn_castFoldableMul - If this value is a multiply that can be folded into
562// other computations (because it has a constant operand), return the
Chris Lattner8c3e7b92004-11-13 19:50:12 +0000563// non-constant operand of the multiply, and set CST to point to the multiplier.
564// Otherwise, return null.
Chris Lattner7fb29e12003-03-11 00:12:48 +0000565//
Chris Lattner8c3e7b92004-11-13 19:50:12 +0000566static inline Value *dyn_castFoldableMul(Value *V, ConstantInt *&CST) {
Chris Lattner03c49532007-01-15 02:27:26 +0000567 if (V->hasOneUse() && V->getType()->isInteger())
Chris Lattner8c3e7b92004-11-13 19:50:12 +0000568 if (Instruction *I = dyn_cast<Instruction>(V)) {
Chris Lattner7fb29e12003-03-11 00:12:48 +0000569 if (I->getOpcode() == Instruction::Mul)
Chris Lattner970136362004-11-15 05:54:07 +0000570 if ((CST = dyn_cast<ConstantInt>(I->getOperand(1))))
Chris Lattner7fb29e12003-03-11 00:12:48 +0000571 return I->getOperand(0);
Chris Lattner8c3e7b92004-11-13 19:50:12 +0000572 if (I->getOpcode() == Instruction::Shl)
Chris Lattner970136362004-11-15 05:54:07 +0000573 if ((CST = dyn_cast<ConstantInt>(I->getOperand(1)))) {
Chris Lattner8c3e7b92004-11-13 19:50:12 +0000574 // The multiplier is really 1 << CST.
Zhou Sheng4961cf12007-03-29 01:57:21 +0000575 uint32_t BitWidth = cast<IntegerType>(V->getType())->getBitWidth();
Zhou Shengb25806f2007-03-30 09:29:48 +0000576 uint32_t CSTVal = CST->getLimitedValue(BitWidth);
Zhou Sheng4961cf12007-03-29 01:57:21 +0000577 CST = ConstantInt::get(APInt(BitWidth, 1).shl(CSTVal));
Chris Lattner8c3e7b92004-11-13 19:50:12 +0000578 return I->getOperand(0);
579 }
580 }
Chris Lattner7fb29e12003-03-11 00:12:48 +0000581 return 0;
Chris Lattner3082c5a2003-02-18 19:28:33 +0000582}
Chris Lattner31ae8632002-08-14 17:51:49 +0000583
Chris Lattner0798af32005-01-13 20:14:25 +0000584/// dyn_castGetElementPtr - If this is a getelementptr instruction or constant
585/// expression, return it.
586static User *dyn_castGetElementPtr(Value *V) {
587 if (isa<GetElementPtrInst>(V)) return cast<User>(V);
588 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
589 if (CE->getOpcode() == Instruction::GetElementPtr)
590 return cast<User>(V);
591 return false;
592}
593
Dan Gohman99b7b3f2008-04-10 18:43:06 +0000594/// getOpcode - If this is an Instruction or a ConstantExpr, return the
595/// opcode value. Otherwise return UserOp1.
596static unsigned getOpcode(User *U) {
597 if (Instruction *I = dyn_cast<Instruction>(U))
598 return I->getOpcode();
599 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(U))
600 return CE->getOpcode();
601 // Use UserOp1 to mean there's no opcode.
602 return Instruction::UserOp1;
603}
604
Reid Spencer80263aa2007-03-25 05:33:51 +0000605/// AddOne - Add one to a ConstantInt
Chris Lattner6862fbd2004-09-29 17:40:11 +0000606static ConstantInt *AddOne(ConstantInt *C) {
Reid Spencer624766f2007-03-25 19:55:33 +0000607 APInt Val(C->getValue());
608 return ConstantInt::get(++Val);
Chris Lattner623826c2004-09-28 21:48:02 +0000609}
Reid Spencer80263aa2007-03-25 05:33:51 +0000610/// SubOne - Subtract one from a ConstantInt
Chris Lattner6862fbd2004-09-29 17:40:11 +0000611static ConstantInt *SubOne(ConstantInt *C) {
Reid Spencer624766f2007-03-25 19:55:33 +0000612 APInt Val(C->getValue());
613 return ConstantInt::get(--Val);
Reid Spencer80263aa2007-03-25 05:33:51 +0000614}
615/// Add - Add two ConstantInts together
616static ConstantInt *Add(ConstantInt *C1, ConstantInt *C2) {
617 return ConstantInt::get(C1->getValue() + C2->getValue());
618}
619/// And - Bitwise AND two ConstantInts together
620static ConstantInt *And(ConstantInt *C1, ConstantInt *C2) {
621 return ConstantInt::get(C1->getValue() & C2->getValue());
622}
623/// Subtract - Subtract one ConstantInt from another
624static ConstantInt *Subtract(ConstantInt *C1, ConstantInt *C2) {
625 return ConstantInt::get(C1->getValue() - C2->getValue());
626}
627/// Multiply - Multiply two ConstantInts together
628static ConstantInt *Multiply(ConstantInt *C1, ConstantInt *C2) {
629 return ConstantInt::get(C1->getValue() * C2->getValue());
Chris Lattner623826c2004-09-28 21:48:02 +0000630}
Nick Lewyckyfefd0202008-02-18 22:48:05 +0000631/// MultiplyOverflows - True if the multiply can not be expressed in an int
632/// this size.
633static bool MultiplyOverflows(ConstantInt *C1, ConstantInt *C2, bool sign) {
634 uint32_t W = C1->getBitWidth();
635 APInt LHSExt = C1->getValue(), RHSExt = C2->getValue();
636 if (sign) {
637 LHSExt.sext(W * 2);
638 RHSExt.sext(W * 2);
639 } else {
640 LHSExt.zext(W * 2);
641 RHSExt.zext(W * 2);
642 }
643
644 APInt MulExt = LHSExt * RHSExt;
645
646 if (sign) {
647 APInt Min = APInt::getSignedMinValue(W).sext(W * 2);
648 APInt Max = APInt::getSignedMaxValue(W).sext(W * 2);
649 return MulExt.slt(Min) || MulExt.sgt(Max);
650 } else
651 return MulExt.ugt(APInt::getLowBitsSet(W * 2, W));
652}
Chris Lattner623826c2004-09-28 21:48:02 +0000653
Chris Lattner4534dd592006-02-09 07:38:58 +0000654/// ComputeMaskedBits - Determine which of the bits specified in Mask are
655/// known to be either zero or one and return them in the KnownZero/KnownOne
Reid Spenceraa696402007-03-08 01:46:38 +0000656/// bit sets. This code only analyzes bits in Mask, in order to short-circuit
657/// processing.
658/// NOTE: we cannot consider 'undef' to be "IsZero" here. The problem is that
659/// we cannot optimize based on the assumption that it is zero without changing
660/// it to be an explicit zero. If we don't change it to zero, other code could
661/// optimized based on the contradictory assumption that it is non-zero.
662/// Because instcombine aggressively folds operations with undef args anyway,
663/// this won't lose us code quality.
Dan Gohman99b7b3f2008-04-10 18:43:06 +0000664void InstCombiner::ComputeMaskedBits(Value *V, const APInt &Mask,
665 APInt& KnownZero, APInt& KnownOne,
666 unsigned Depth) {
Zhou Shengaf4341d2007-03-13 02:23:10 +0000667 assert(V && "No Value?");
668 assert(Depth <= 6 && "Limit Search Depth");
Reid Spenceraa696402007-03-08 01:46:38 +0000669 uint32_t BitWidth = Mask.getBitWidth();
Dan Gohman99b7b3f2008-04-10 18:43:06 +0000670 assert((V->getType()->isInteger() || isa<PointerType>(V->getType())) &&
671 "Not integer or pointer type!");
672 assert((!TD || TD->getTypeSizeInBits(V->getType()) == BitWidth) &&
673 (!isa<IntegerType>(V->getType()) ||
674 V->getType()->getPrimitiveSizeInBits() == BitWidth) &&
Zhou Shengaf4341d2007-03-13 02:23:10 +0000675 KnownZero.getBitWidth() == BitWidth &&
Reid Spenceraa696402007-03-08 01:46:38 +0000676 KnownOne.getBitWidth() == BitWidth &&
Zhou Sheng57e3f732007-03-28 02:19:03 +0000677 "V, Mask, KnownOne and KnownZero should have same BitWidth");
Reid Spenceraa696402007-03-08 01:46:38 +0000678 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
679 // We know all of the bits for a constant!
Zhou Shengaf4341d2007-03-13 02:23:10 +0000680 KnownOne = CI->getValue() & Mask;
Reid Spenceraa696402007-03-08 01:46:38 +0000681 KnownZero = ~KnownOne & Mask;
682 return;
683 }
Dan Gohman99b7b3f2008-04-10 18:43:06 +0000684 // Null is all-zeros.
685 if (isa<ConstantPointerNull>(V)) {
686 KnownOne.clear();
687 KnownZero = Mask;
688 return;
689 }
690 // The address of an aligned GlobalValue has trailing zeros.
691 if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
692 unsigned Align = GV->getAlignment();
693 if (Align == 0 && TD && GV->getType()->getElementType()->isSized())
694 Align = TD->getPrefTypeAlignment(GV->getType()->getElementType());
695 if (Align > 0)
696 KnownZero = Mask & APInt::getLowBitsSet(BitWidth,
697 CountTrailingZeros_32(Align));
698 else
699 KnownZero.clear();
700 KnownOne.clear();
701 return;
702 }
Reid Spenceraa696402007-03-08 01:46:38 +0000703
Dan Gohman72ec3f42008-04-28 17:02:21 +0000704 KnownZero.clear(); KnownOne.clear(); // Start out not knowing anything.
705
Reid Spenceraa696402007-03-08 01:46:38 +0000706 if (Depth == 6 || Mask == 0)
707 return; // Limit search depth.
708
Dan Gohman99b7b3f2008-04-10 18:43:06 +0000709 User *I = dyn_cast<User>(V);
Reid Spenceraa696402007-03-08 01:46:38 +0000710 if (!I) return;
711
712 APInt KnownZero2(KnownZero), KnownOne2(KnownOne);
Dan Gohman99b7b3f2008-04-10 18:43:06 +0000713 switch (getOpcode(I)) {
714 default: break;
Reid Spencerd8aad612007-03-25 02:03:12 +0000715 case Instruction::And: {
Reid Spenceraa696402007-03-08 01:46:38 +0000716 // If either the LHS or the RHS are Zero, the result is zero.
717 ComputeMaskedBits(I->getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Reid Spencerd8aad612007-03-25 02:03:12 +0000718 APInt Mask2(Mask & ~KnownZero);
719 ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
Reid Spenceraa696402007-03-08 01:46:38 +0000720 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
721 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
722
723 // Output known-1 bits are only known if set in both the LHS & RHS.
724 KnownOne &= KnownOne2;
725 // Output known-0 are known to be clear if zero in either the LHS | RHS.
726 KnownZero |= KnownZero2;
727 return;
Reid Spencerd8aad612007-03-25 02:03:12 +0000728 }
729 case Instruction::Or: {
Reid Spenceraa696402007-03-08 01:46:38 +0000730 ComputeMaskedBits(I->getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Reid Spencerd8aad612007-03-25 02:03:12 +0000731 APInt Mask2(Mask & ~KnownOne);
732 ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
Reid Spenceraa696402007-03-08 01:46:38 +0000733 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
734 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
735
736 // Output known-0 bits are only known if clear in both the LHS & RHS.
737 KnownZero &= KnownZero2;
738 // Output known-1 are known to be set if set in either the LHS | RHS.
739 KnownOne |= KnownOne2;
740 return;
Reid Spencerd8aad612007-03-25 02:03:12 +0000741 }
Reid Spenceraa696402007-03-08 01:46:38 +0000742 case Instruction::Xor: {
743 ComputeMaskedBits(I->getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
744 ComputeMaskedBits(I->getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
745 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
746 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
747
748 // Output known-0 bits are known if clear or set in both the LHS & RHS.
749 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
750 // Output known-1 are known to be set if set in only one of the LHS, RHS.
751 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
752 KnownZero = KnownZeroOut;
753 return;
754 }
Dan Gohman99b7b3f2008-04-10 18:43:06 +0000755 case Instruction::Mul: {
756 APInt Mask2 = APInt::getAllOnesValue(BitWidth);
757 ComputeMaskedBits(I->getOperand(1), Mask2, KnownZero, KnownOne, Depth+1);
758 ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
759 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
760 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
761
762 // If low bits are zero in either operand, output low known-0 bits.
Dan Gohman72ec3f42008-04-28 17:02:21 +0000763 // Also compute a conserative estimate for high known-0 bits.
Dan Gohman99b7b3f2008-04-10 18:43:06 +0000764 // More trickiness is possible, but this is sufficient for the
765 // interesting case of alignment computation.
766 KnownOne.clear();
767 unsigned TrailZ = KnownZero.countTrailingOnes() +
768 KnownZero2.countTrailingOnes();
Dan Gohman72ec3f42008-04-28 17:02:21 +0000769 unsigned LeadZ = std::max(KnownZero.countLeadingOnes() +
770 KnownZero2.countLeadingOnes() +
771 1, BitWidth) - BitWidth;
772
Dan Gohman99b7b3f2008-04-10 18:43:06 +0000773 TrailZ = std::min(TrailZ, BitWidth);
Dan Gohman72ec3f42008-04-28 17:02:21 +0000774 LeadZ = std::min(LeadZ, BitWidth);
775 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
776 APInt::getHighBitsSet(BitWidth, LeadZ);
Dan Gohman99b7b3f2008-04-10 18:43:06 +0000777 KnownZero &= Mask;
778 return;
779 }
Dan Gohman72ec3f42008-04-28 17:02:21 +0000780 case Instruction::UDiv: {
781 // For the purposes of computing leading zeros we can conservatively
782 // treat a udiv as a logical right shift by the power of 2 known to
783 // be greater than the denominator.
784 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
785 ComputeMaskedBits(I->getOperand(0),
786 AllOnes, KnownZero2, KnownOne2, Depth+1);
787 unsigned LeadZ = KnownZero2.countLeadingOnes();
788
789 KnownOne2.clear();
790 KnownZero2.clear();
791 ComputeMaskedBits(I->getOperand(1),
792 AllOnes, KnownZero2, KnownOne2, Depth+1);
793 LeadZ = std::min(BitWidth,
794 LeadZ + BitWidth - KnownOne2.countLeadingZeros());
795
796 KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ) & Mask;
797 return;
798 }
Reid Spenceraa696402007-03-08 01:46:38 +0000799 case Instruction::Select:
800 ComputeMaskedBits(I->getOperand(2), Mask, KnownZero, KnownOne, Depth+1);
801 ComputeMaskedBits(I->getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1);
802 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
803 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
804
805 // Only known if known in both the LHS and RHS.
806 KnownOne &= KnownOne2;
807 KnownZero &= KnownZero2;
808 return;
809 case Instruction::FPTrunc:
810 case Instruction::FPExt:
811 case Instruction::FPToUI:
812 case Instruction::FPToSI:
813 case Instruction::SIToFP:
Reid Spenceraa696402007-03-08 01:46:38 +0000814 case Instruction::UIToFP:
Dan Gohman99b7b3f2008-04-10 18:43:06 +0000815 return; // Can't work with floating point.
816 case Instruction::PtrToInt:
Reid Spenceraa696402007-03-08 01:46:38 +0000817 case Instruction::IntToPtr:
Dan Gohman99b7b3f2008-04-10 18:43:06 +0000818 // We can't handle these if we don't know the pointer size.
819 if (!TD) return;
820 // Fall through and handle them the same as zext/trunc.
821 case Instruction::ZExt:
Zhou Shengaf4341d2007-03-13 02:23:10 +0000822 case Instruction::Trunc: {
Reid Spenceraa696402007-03-08 01:46:38 +0000823 // All these have integer operands
Dan Gohman99b7b3f2008-04-10 18:43:06 +0000824 const Type *SrcTy = I->getOperand(0)->getType();
825 uint32_t SrcBitWidth = TD ?
826 TD->getTypeSizeInBits(SrcTy) :
827 SrcTy->getPrimitiveSizeInBits();
Zhou Sheng57e3f732007-03-28 02:19:03 +0000828 APInt MaskIn(Mask);
Dan Gohman99b7b3f2008-04-10 18:43:06 +0000829 MaskIn.zextOrTrunc(SrcBitWidth);
830 KnownZero.zextOrTrunc(SrcBitWidth);
831 KnownOne.zextOrTrunc(SrcBitWidth);
Zhou Sheng57e3f732007-03-28 02:19:03 +0000832 ComputeMaskedBits(I->getOperand(0), MaskIn, KnownZero, KnownOne, Depth+1);
Dan Gohman99b7b3f2008-04-10 18:43:06 +0000833 KnownZero.zextOrTrunc(BitWidth);
834 KnownOne.zextOrTrunc(BitWidth);
835 // Any top bits are known to be zero.
836 if (BitWidth > SrcBitWidth)
837 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth);
Reid Spenceraa696402007-03-08 01:46:38 +0000838 return;
Zhou Shengaf4341d2007-03-13 02:23:10 +0000839 }
Reid Spenceraa696402007-03-08 01:46:38 +0000840 case Instruction::BitCast: {
841 const Type *SrcTy = I->getOperand(0)->getType();
Dan Gohman99b7b3f2008-04-10 18:43:06 +0000842 if (SrcTy->isInteger() || isa<PointerType>(SrcTy)) {
Reid Spenceraa696402007-03-08 01:46:38 +0000843 ComputeMaskedBits(I->getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
844 return;
845 }
846 break;
847 }
Reid Spenceraa696402007-03-08 01:46:38 +0000848 case Instruction::SExt: {
849 // Compute the bits in the result that are not present in the input.
850 const IntegerType *SrcTy = cast<IntegerType>(I->getOperand(0)->getType());
Zhou Shengaf4341d2007-03-13 02:23:10 +0000851 uint32_t SrcBitWidth = SrcTy->getBitWidth();
Reid Spencercd99fbd2007-03-25 04:26:16 +0000852
Zhou Sheng57e3f732007-03-28 02:19:03 +0000853 APInt MaskIn(Mask);
854 MaskIn.trunc(SrcBitWidth);
855 KnownZero.trunc(SrcBitWidth);
856 KnownOne.trunc(SrcBitWidth);
857 ComputeMaskedBits(I->getOperand(0), MaskIn, KnownZero, KnownOne, Depth+1);
Reid Spenceraa696402007-03-08 01:46:38 +0000858 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Zhou Shengaf4341d2007-03-13 02:23:10 +0000859 KnownZero.zext(BitWidth);
860 KnownOne.zext(BitWidth);
Reid Spenceraa696402007-03-08 01:46:38 +0000861
862 // If the sign bit of the input is known set or clear, then we know the
863 // top bits of the result.
Zhou Sheng57e3f732007-03-28 02:19:03 +0000864 if (KnownZero[SrcBitWidth-1]) // Input sign bit known zero
Zhou Sheng117477e2007-03-28 17:38:21 +0000865 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth);
Zhou Sheng57e3f732007-03-28 02:19:03 +0000866 else if (KnownOne[SrcBitWidth-1]) // Input sign bit known set
Zhou Sheng117477e2007-03-28 17:38:21 +0000867 KnownOne |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth);
Reid Spenceraa696402007-03-08 01:46:38 +0000868 return;
869 }
870 case Instruction::Shl:
871 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
872 if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
Zhou Shengb25806f2007-03-30 09:29:48 +0000873 uint64_t ShiftAmt = SA->getLimitedValue(BitWidth);
Reid Spencerd8aad612007-03-25 02:03:12 +0000874 APInt Mask2(Mask.lshr(ShiftAmt));
875 ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero, KnownOne, Depth+1);
Reid Spenceraa696402007-03-08 01:46:38 +0000876 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Zhou Shengb3e00c42007-03-12 05:44:52 +0000877 KnownZero <<= ShiftAmt;
878 KnownOne <<= ShiftAmt;
Reid Spencer624766f2007-03-25 19:55:33 +0000879 KnownZero |= APInt::getLowBitsSet(BitWidth, ShiftAmt); // low bits known 0
Reid Spenceraa696402007-03-08 01:46:38 +0000880 return;
881 }
882 break;
883 case Instruction::LShr:
884 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
885 if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
886 // Compute the new bits that are at the top now.
Zhou Shengb25806f2007-03-30 09:29:48 +0000887 uint64_t ShiftAmt = SA->getLimitedValue(BitWidth);
Reid Spenceraa696402007-03-08 01:46:38 +0000888
889 // Unsigned shift right.
Reid Spencerd8aad612007-03-25 02:03:12 +0000890 APInt Mask2(Mask.shl(ShiftAmt));
891 ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero,KnownOne,Depth+1);
Reid Spenceraa696402007-03-08 01:46:38 +0000892 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
893 KnownZero = APIntOps::lshr(KnownZero, ShiftAmt);
894 KnownOne = APIntOps::lshr(KnownOne, ShiftAmt);
Zhou Sheng57e3f732007-03-28 02:19:03 +0000895 // high bits known zero.
896 KnownZero |= APInt::getHighBitsSet(BitWidth, ShiftAmt);
Reid Spenceraa696402007-03-08 01:46:38 +0000897 return;
898 }
899 break;
900 case Instruction::AShr:
Zhou Sheng57e3f732007-03-28 02:19:03 +0000901 // (ashr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
Reid Spenceraa696402007-03-08 01:46:38 +0000902 if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
903 // Compute the new bits that are at the top now.
Zhou Shengb25806f2007-03-30 09:29:48 +0000904 uint64_t ShiftAmt = SA->getLimitedValue(BitWidth);
Reid Spenceraa696402007-03-08 01:46:38 +0000905
906 // Signed shift right.
Reid Spencerd8aad612007-03-25 02:03:12 +0000907 APInt Mask2(Mask.shl(ShiftAmt));
908 ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero,KnownOne,Depth+1);
Reid Spenceraa696402007-03-08 01:46:38 +0000909 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
910 KnownZero = APIntOps::lshr(KnownZero, ShiftAmt);
911 KnownOne = APIntOps::lshr(KnownOne, ShiftAmt);
912
Zhou Sheng57e3f732007-03-28 02:19:03 +0000913 APInt HighBits(APInt::getHighBitsSet(BitWidth, ShiftAmt));
914 if (KnownZero[BitWidth-ShiftAmt-1]) // New bits are known zero.
Reid Spenceraa696402007-03-08 01:46:38 +0000915 KnownZero |= HighBits;
Zhou Sheng57e3f732007-03-28 02:19:03 +0000916 else if (KnownOne[BitWidth-ShiftAmt-1]) // New bits are known one.
Reid Spenceraa696402007-03-08 01:46:38 +0000917 KnownOne |= HighBits;
Reid Spenceraa696402007-03-08 01:46:38 +0000918 return;
919 }
920 break;
Dan Gohman99b7b3f2008-04-10 18:43:06 +0000921 case Instruction::Sub: {
922 if (ConstantInt *CLHS = dyn_cast<ConstantInt>(I->getOperand(0))) {
923 // We know that the top bits of C-X are clear if X contains less bits
924 // than C (i.e. no wrap-around can happen). For example, 20-X is
925 // positive if we can prove that X is >= 0 and < 16.
926 if (!CLHS->getValue().isNegative()) {
927 unsigned NLZ = (CLHS->getValue()+1).countLeadingZeros();
928 // NLZ can't be BitWidth with no sign bit
929 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +0000930 ComputeMaskedBits(I->getOperand(1), MaskV, KnownZero2, KnownOne2,
931 Depth+1);
Dan Gohman99b7b3f2008-04-10 18:43:06 +0000932
Dan Gohman72ec3f42008-04-28 17:02:21 +0000933 // If all of the MaskV bits are known to be zero, then we know the
934 // output top bits are zero, because we now know that the output is
935 // from [0-C].
936 if ((KnownZero2 & MaskV) == MaskV) {
Dan Gohman99b7b3f2008-04-10 18:43:06 +0000937 unsigned NLZ2 = CLHS->getValue().countLeadingZeros();
938 // Top bits known zero.
939 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2) & Mask;
Dan Gohman99b7b3f2008-04-10 18:43:06 +0000940 }
Dan Gohman99b7b3f2008-04-10 18:43:06 +0000941 }
942 }
943 }
944 // fall through
Duncan Sandsc9e09a02008-03-21 08:32:17 +0000945 case Instruction::Add: {
Chris Lattnerc44160c2008-03-21 05:19:58 +0000946 // Output known-0 bits are known if clear or set in both the low clear bits
947 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
948 // low 3 bits clear.
Dan Gohman72ec3f42008-04-28 17:02:21 +0000949 APInt Mask2 = APInt::getLowBitsSet(BitWidth, Mask.countTrailingOnes());
950 ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
951 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
952 unsigned KnownZeroOut = KnownZero2.countTrailingOnes();
953
954 ComputeMaskedBits(I->getOperand(1), Mask2, KnownZero2, KnownOne2, Depth+1);
955 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
956 KnownZeroOut = std::min(KnownZeroOut,
957 KnownZero2.countTrailingOnes());
958
959 KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroOut);
Chris Lattnerc44160c2008-03-21 05:19:58 +0000960 return;
Duncan Sandsc9e09a02008-03-21 08:32:17 +0000961 }
Nick Lewyckyd0b62a12008-03-06 06:48:30 +0000962 case Instruction::SRem:
963 if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) {
964 APInt RA = Rem->getValue();
965 if (RA.isPowerOf2() || (-RA).isPowerOf2()) {
966 APInt LowBits = RA.isStrictlyPositive() ? ((RA - 1) | RA) : ~RA;
967 APInt Mask2 = LowBits | APInt::getSignBit(BitWidth);
968 ComputeMaskedBits(I->getOperand(0), Mask2,KnownZero2,KnownOne2,Depth+1);
969
970 // The sign of a remainder is equal to the sign of the first
971 // operand (zero being positive).
972 if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
973 KnownZero2 |= ~LowBits;
974 else if (KnownOne2[BitWidth-1])
975 KnownOne2 |= ~LowBits;
976
977 KnownZero |= KnownZero2 & Mask;
978 KnownOne |= KnownOne2 & Mask;
979
980 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
981 }
982 }
983 break;
Dan Gohman72ec3f42008-04-28 17:02:21 +0000984 case Instruction::URem: {
Nick Lewyckyd0b62a12008-03-06 06:48:30 +0000985 if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) {
986 APInt RA = Rem->getValue();
987 if (RA.isStrictlyPositive() && RA.isPowerOf2()) {
988 APInt LowBits = (RA - 1) | RA;
989 APInt Mask2 = LowBits & Mask;
990 KnownZero |= ~LowBits & Mask;
991 ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero, KnownOne,Depth+1);
992 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
Dan Gohman72ec3f42008-04-28 17:02:21 +0000993 break;
Nick Lewyckyd0b62a12008-03-06 06:48:30 +0000994 }
Nick Lewyckyd0b62a12008-03-06 06:48:30 +0000995 }
Dan Gohman72ec3f42008-04-28 17:02:21 +0000996
997 // Since the result is less than or equal to either operand, any leading
998 // zero bits in either operand must also exist in the result.
999 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
1000 ComputeMaskedBits(I->getOperand(0), AllOnes, KnownZero, KnownOne,
1001 Depth+1);
1002 ComputeMaskedBits(I->getOperand(1), AllOnes, KnownZero2, KnownOne2,
1003 Depth+1);
1004
1005 uint32_t Leaders = std::max(KnownZero.countLeadingOnes(),
1006 KnownZero2.countLeadingOnes());
1007 KnownOne.clear();
1008 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders) & Mask;
Nick Lewyckyd0b62a12008-03-06 06:48:30 +00001009 break;
Dan Gohman72ec3f42008-04-28 17:02:21 +00001010 }
Dan Gohman99b7b3f2008-04-10 18:43:06 +00001011
1012 case Instruction::Alloca:
1013 case Instruction::Malloc: {
1014 AllocationInst *AI = cast<AllocationInst>(V);
1015 unsigned Align = AI->getAlignment();
1016 if (Align == 0 && TD) {
1017 if (isa<AllocaInst>(AI))
1018 Align = TD->getPrefTypeAlignment(AI->getType()->getElementType());
1019 else if (isa<MallocInst>(AI)) {
1020 // Malloc returns maximally aligned memory.
1021 Align = TD->getABITypeAlignment(AI->getType()->getElementType());
1022 Align =
1023 std::max(Align,
1024 (unsigned)TD->getABITypeAlignment(Type::DoubleTy));
1025 Align =
1026 std::max(Align,
1027 (unsigned)TD->getABITypeAlignment(Type::Int64Ty));
1028 }
1029 }
1030
1031 if (Align > 0)
1032 KnownZero = Mask & APInt::getLowBitsSet(BitWidth,
1033 CountTrailingZeros_32(Align));
1034 break;
1035 }
1036 case Instruction::GetElementPtr: {
1037 // Analyze all of the subscripts of this getelementptr instruction
1038 // to determine if we can prove known low zero bits.
1039 APInt LocalMask = APInt::getAllOnesValue(BitWidth);
1040 APInt LocalKnownZero(BitWidth, 0), LocalKnownOne(BitWidth, 0);
1041 ComputeMaskedBits(I->getOperand(0), LocalMask,
1042 LocalKnownZero, LocalKnownOne, Depth+1);
1043 unsigned TrailZ = LocalKnownZero.countTrailingOnes();
1044
1045 gep_type_iterator GTI = gep_type_begin(I);
1046 for (unsigned i = 1, e = I->getNumOperands(); i != e; ++i, ++GTI) {
1047 Value *Index = I->getOperand(i);
1048 if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
1049 // Handle struct member offset arithmetic.
1050 if (!TD) return;
1051 const StructLayout *SL = TD->getStructLayout(STy);
1052 unsigned Idx = cast<ConstantInt>(Index)->getZExtValue();
1053 uint64_t Offset = SL->getElementOffset(Idx);
1054 TrailZ = std::min(TrailZ,
1055 CountTrailingZeros_64(Offset));
1056 } else {
1057 // Handle array index arithmetic.
1058 const Type *IndexedTy = GTI.getIndexedType();
1059 if (!IndexedTy->isSized()) return;
1060 unsigned GEPOpiBits = Index->getType()->getPrimitiveSizeInBits();
1061 uint64_t TypeSize = TD ? TD->getABITypeSize(IndexedTy) : 1;
1062 LocalMask = APInt::getAllOnesValue(GEPOpiBits);
1063 LocalKnownZero = LocalKnownOne = APInt(GEPOpiBits, 0);
1064 ComputeMaskedBits(Index, LocalMask,
1065 LocalKnownZero, LocalKnownOne, Depth+1);
1066 TrailZ = std::min(TrailZ,
1067 CountTrailingZeros_64(TypeSize) +
1068 LocalKnownZero.countTrailingOnes());
1069 }
1070 }
1071
1072 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) & Mask;
1073 break;
1074 }
1075 case Instruction::PHI: {
1076 PHINode *P = cast<PHINode>(I);
1077 // Handle the case of a simple two-predecessor recurrence PHI.
1078 // There's a lot more that could theoretically be done here, but
1079 // this is sufficient to catch some interesting cases.
1080 if (P->getNumIncomingValues() == 2) {
1081 for (unsigned i = 0; i != 2; ++i) {
1082 Value *L = P->getIncomingValue(i);
1083 Value *R = P->getIncomingValue(!i);
1084 User *LU = dyn_cast<User>(L);
1085 unsigned Opcode = LU ? getOpcode(LU) : (unsigned)Instruction::UserOp1;
1086 // Check for operations that have the property that if
1087 // both their operands have low zero bits, the result
1088 // will have low zero bits.
1089 if (Opcode == Instruction::Add ||
1090 Opcode == Instruction::Sub ||
1091 Opcode == Instruction::And ||
1092 Opcode == Instruction::Or ||
1093 Opcode == Instruction::Mul) {
1094 Value *LL = LU->getOperand(0);
1095 Value *LR = LU->getOperand(1);
1096 // Find a recurrence.
1097 if (LL == I)
1098 L = LR;
1099 else if (LR == I)
1100 L = LL;
1101 else
1102 break;
1103 // Ok, we have a PHI of the form L op= R. Check for low
1104 // zero bits.
1105 APInt Mask2 = APInt::getAllOnesValue(BitWidth);
1106 ComputeMaskedBits(R, Mask2, KnownZero2, KnownOne2, Depth+1);
1107 Mask2 = APInt::getLowBitsSet(BitWidth,
1108 KnownZero2.countTrailingOnes());
1109 KnownOne2.clear();
1110 KnownZero2.clear();
1111 ComputeMaskedBits(L, Mask2, KnownZero2, KnownOne2, Depth+1);
1112 KnownZero = Mask &
1113 APInt::getLowBitsSet(BitWidth,
1114 KnownZero2.countTrailingOnes());
1115 break;
1116 }
1117 }
1118 }
1119 break;
1120 }
Dan Gohman72ec3f42008-04-28 17:02:21 +00001121 case Instruction::Call:
1122 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
1123 switch (II->getIntrinsicID()) {
1124 default: break;
1125 case Intrinsic::ctpop:
1126 case Intrinsic::ctlz:
1127 case Intrinsic::cttz: {
1128 unsigned LowBits = Log2_32(BitWidth)+1;
1129 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
1130 break;
1131 }
1132 }
1133 }
1134 break;
Reid Spenceraa696402007-03-08 01:46:38 +00001135 }
1136}
1137
Reid Spencerbb5741f2007-03-08 01:52:58 +00001138/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1139/// this predicate to simplify operations downstream. Mask is known to be zero
1140/// for bits that V cannot have.
Dan Gohman99b7b3f2008-04-10 18:43:06 +00001141bool InstCombiner::MaskedValueIsZero(Value *V, const APInt& Mask,
1142 unsigned Depth) {
Zhou Shengbe171ee2007-03-12 16:54:56 +00001143 APInt KnownZero(Mask.getBitWidth(), 0), KnownOne(Mask.getBitWidth(), 0);
Reid Spencerbb5741f2007-03-08 01:52:58 +00001144 ComputeMaskedBits(V, Mask, KnownZero, KnownOne, Depth);
1145 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1146 return (KnownZero & Mask) == Mask;
1147}
1148
Chris Lattner0157e7f2006-02-11 09:31:47 +00001149/// ShrinkDemandedConstant - Check to see if the specified operand of the
1150/// specified instruction is a constant integer. If so, check to see if there
1151/// are any bits set in the constant that are not demanded. If so, shrink the
1152/// constant and return true.
1153static bool ShrinkDemandedConstant(Instruction *I, unsigned OpNo,
Reid Spencerd9281782007-03-12 17:15:10 +00001154 APInt Demanded) {
1155 assert(I && "No instruction?");
1156 assert(OpNo < I->getNumOperands() && "Operand index too large");
1157
1158 // If the operand is not a constant integer, nothing to do.
1159 ConstantInt *OpC = dyn_cast<ConstantInt>(I->getOperand(OpNo));
1160 if (!OpC) return false;
1161
1162 // If there are no bits set that aren't demanded, nothing to do.
1163 Demanded.zextOrTrunc(OpC->getValue().getBitWidth());
1164 if ((~Demanded & OpC->getValue()) == 0)
1165 return false;
1166
1167 // This instruction is producing bits that are not demanded. Shrink the RHS.
1168 Demanded &= OpC->getValue();
1169 I->setOperand(OpNo, ConstantInt::get(Demanded));
1170 return true;
1171}
1172
Chris Lattneree0f2802006-02-12 02:07:56 +00001173// ComputeSignedMinMaxValuesFromKnownBits - Given a signed integer type and a
1174// set of known zero and one bits, compute the maximum and minimum values that
1175// could have the specified known zero and known one bits, returning them in
1176// min/max.
1177static void ComputeSignedMinMaxValuesFromKnownBits(const Type *Ty,
Reid Spencerc3e3b8a2007-03-22 20:36:03 +00001178 const APInt& KnownZero,
1179 const APInt& KnownOne,
1180 APInt& Min, APInt& Max) {
1181 uint32_t BitWidth = cast<IntegerType>(Ty)->getBitWidth();
1182 assert(KnownZero.getBitWidth() == BitWidth &&
1183 KnownOne.getBitWidth() == BitWidth &&
1184 Min.getBitWidth() == BitWidth && Max.getBitWidth() == BitWidth &&
1185 "Ty, KnownZero, KnownOne and Min, Max must have equal bitwidth.");
Reid Spencercd99fbd2007-03-25 04:26:16 +00001186 APInt UnknownBits = ~(KnownZero|KnownOne);
Chris Lattneree0f2802006-02-12 02:07:56 +00001187
Chris Lattneree0f2802006-02-12 02:07:56 +00001188 // The minimum value is when all unknown bits are zeros, EXCEPT for the sign
1189 // bit if it is unknown.
1190 Min = KnownOne;
1191 Max = KnownOne|UnknownBits;
1192
Zhou Shengc2d33092007-03-28 05:15:57 +00001193 if (UnknownBits[BitWidth-1]) { // Sign bit is unknown
Zhou Sheng9bc8ab12007-04-02 13:45:30 +00001194 Min.set(BitWidth-1);
1195 Max.clear(BitWidth-1);
Chris Lattneree0f2802006-02-12 02:07:56 +00001196 }
Chris Lattneree0f2802006-02-12 02:07:56 +00001197}
1198
1199// ComputeUnsignedMinMaxValuesFromKnownBits - Given an unsigned integer type and
1200// a set of known zero and one bits, compute the maximum and minimum values that
1201// could have the specified known zero and known one bits, returning them in
1202// min/max.
1203static void ComputeUnsignedMinMaxValuesFromKnownBits(const Type *Ty,
Chris Lattnerf0da7972007-08-05 08:47:58 +00001204 const APInt &KnownZero,
1205 const APInt &KnownOne,
1206 APInt &Min, APInt &Max) {
1207 uint32_t BitWidth = cast<IntegerType>(Ty)->getBitWidth(); BitWidth = BitWidth;
Reid Spencerc3e3b8a2007-03-22 20:36:03 +00001208 assert(KnownZero.getBitWidth() == BitWidth &&
1209 KnownOne.getBitWidth() == BitWidth &&
1210 Min.getBitWidth() == BitWidth && Max.getBitWidth() &&
1211 "Ty, KnownZero, KnownOne and Min, Max must have equal bitwidth.");
Reid Spencercd99fbd2007-03-25 04:26:16 +00001212 APInt UnknownBits = ~(KnownZero|KnownOne);
Chris Lattneree0f2802006-02-12 02:07:56 +00001213
1214 // The minimum value is when the unknown bits are all zeros.
1215 Min = KnownOne;
1216 // The maximum value is when the unknown bits are all ones.
1217 Max = KnownOne|UnknownBits;
1218}
Chris Lattner0157e7f2006-02-11 09:31:47 +00001219
Reid Spencer1791f232007-03-12 17:25:59 +00001220/// SimplifyDemandedBits - This function attempts to replace V with a simpler
1221/// value based on the demanded bits. When this function is called, it is known
1222/// that only the bits set in DemandedMask of the result of V are ever used
1223/// downstream. Consequently, depending on the mask and V, it may be possible
1224/// to replace V with a constant or one of its operands. In such cases, this
1225/// function does the replacement and returns true. In all other cases, it
1226/// returns false after analyzing the expression and setting KnownOne and known
1227/// to be one in the expression. KnownZero contains all the bits that are known
1228/// to be zero in the expression. These are provided to potentially allow the
1229/// caller (which might recursively be SimplifyDemandedBits itself) to simplify
1230/// the expression. KnownOne and KnownZero always follow the invariant that
1231/// KnownOne & KnownZero == 0. That is, a bit can't be both 1 and 0. Note that
1232/// the bits in KnownOne and KnownZero may only be accurate for those bits set
1233/// in DemandedMask. Note also that the bitwidth of V, DemandedMask, KnownZero
1234/// and KnownOne must all be the same.
1235bool InstCombiner::SimplifyDemandedBits(Value *V, APInt DemandedMask,
1236 APInt& KnownZero, APInt& KnownOne,
1237 unsigned Depth) {
1238 assert(V != 0 && "Null pointer of Value???");
1239 assert(Depth <= 6 && "Limit Search Depth");
1240 uint32_t BitWidth = DemandedMask.getBitWidth();
1241 const IntegerType *VTy = cast<IntegerType>(V->getType());
1242 assert(VTy->getBitWidth() == BitWidth &&
1243 KnownZero.getBitWidth() == BitWidth &&
1244 KnownOne.getBitWidth() == BitWidth &&
1245 "Value *V, DemandedMask, KnownZero and KnownOne \
1246 must have same BitWidth");
1247 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
1248 // We know all of the bits for a constant!
1249 KnownOne = CI->getValue() & DemandedMask;
1250 KnownZero = ~KnownOne & DemandedMask;
1251 return false;
1252 }
1253
Zhou Shengb9128442007-03-14 03:21:24 +00001254 KnownZero.clear();
1255 KnownOne.clear();
Reid Spencer1791f232007-03-12 17:25:59 +00001256 if (!V->hasOneUse()) { // Other users may use these bits.
1257 if (Depth != 0) { // Not at the root.
1258 // Just compute the KnownZero/KnownOne bits to simplify things downstream.
1259 ComputeMaskedBits(V, DemandedMask, KnownZero, KnownOne, Depth);
1260 return false;
1261 }
1262 // If this is the root being simplified, allow it to have multiple uses,
1263 // just set the DemandedMask to all bits.
1264 DemandedMask = APInt::getAllOnesValue(BitWidth);
1265 } else if (DemandedMask == 0) { // Not demanding any bits from V.
1266 if (V != UndefValue::get(VTy))
1267 return UpdateValueUsesWith(V, UndefValue::get(VTy));
1268 return false;
1269 } else if (Depth == 6) { // Limit search depth.
1270 return false;
1271 }
1272
1273 Instruction *I = dyn_cast<Instruction>(V);
1274 if (!I) return false; // Only analyze instructions.
1275
Reid Spencer1791f232007-03-12 17:25:59 +00001276 APInt LHSKnownZero(BitWidth, 0), LHSKnownOne(BitWidth, 0);
1277 APInt &RHSKnownZero = KnownZero, &RHSKnownOne = KnownOne;
1278 switch (I->getOpcode()) {
Dan Gohman72ec3f42008-04-28 17:02:21 +00001279 default:
1280 ComputeMaskedBits(V, DemandedMask, RHSKnownZero, RHSKnownOne, Depth);
1281 break;
Reid Spencer1791f232007-03-12 17:25:59 +00001282 case Instruction::And:
1283 // If either the LHS or the RHS are Zero, the result is zero.
1284 if (SimplifyDemandedBits(I->getOperand(1), DemandedMask,
1285 RHSKnownZero, RHSKnownOne, Depth+1))
1286 return true;
1287 assert((RHSKnownZero & RHSKnownOne) == 0 &&
1288 "Bits known to be one AND zero?");
1289
1290 // If something is known zero on the RHS, the bits aren't demanded on the
1291 // LHS.
1292 if (SimplifyDemandedBits(I->getOperand(0), DemandedMask & ~RHSKnownZero,
1293 LHSKnownZero, LHSKnownOne, Depth+1))
1294 return true;
1295 assert((LHSKnownZero & LHSKnownOne) == 0 &&
1296 "Bits known to be one AND zero?");
1297
1298 // If all of the demanded bits are known 1 on one side, return the other.
1299 // These bits cannot contribute to the result of the 'and'.
1300 if ((DemandedMask & ~LHSKnownZero & RHSKnownOne) ==
1301 (DemandedMask & ~LHSKnownZero))
1302 return UpdateValueUsesWith(I, I->getOperand(0));
1303 if ((DemandedMask & ~RHSKnownZero & LHSKnownOne) ==
1304 (DemandedMask & ~RHSKnownZero))
1305 return UpdateValueUsesWith(I, I->getOperand(1));
1306
1307 // If all of the demanded bits in the inputs are known zeros, return zero.
1308 if ((DemandedMask & (RHSKnownZero|LHSKnownZero)) == DemandedMask)
1309 return UpdateValueUsesWith(I, Constant::getNullValue(VTy));
1310
1311 // If the RHS is a constant, see if we can simplify it.
1312 if (ShrinkDemandedConstant(I, 1, DemandedMask & ~LHSKnownZero))
1313 return UpdateValueUsesWith(I, I);
1314
1315 // Output known-1 bits are only known if set in both the LHS & RHS.
1316 RHSKnownOne &= LHSKnownOne;
1317 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1318 RHSKnownZero |= LHSKnownZero;
1319 break;
1320 case Instruction::Or:
1321 // If either the LHS or the RHS are One, the result is One.
1322 if (SimplifyDemandedBits(I->getOperand(1), DemandedMask,
1323 RHSKnownZero, RHSKnownOne, Depth+1))
1324 return true;
1325 assert((RHSKnownZero & RHSKnownOne) == 0 &&
1326 "Bits known to be one AND zero?");
1327 // If something is known one on the RHS, the bits aren't demanded on the
1328 // LHS.
1329 if (SimplifyDemandedBits(I->getOperand(0), DemandedMask & ~RHSKnownOne,
1330 LHSKnownZero, LHSKnownOne, Depth+1))
1331 return true;
1332 assert((LHSKnownZero & LHSKnownOne) == 0 &&
1333 "Bits known to be one AND zero?");
1334
1335 // If all of the demanded bits are known zero on one side, return the other.
1336 // These bits cannot contribute to the result of the 'or'.
1337 if ((DemandedMask & ~LHSKnownOne & RHSKnownZero) ==
1338 (DemandedMask & ~LHSKnownOne))
1339 return UpdateValueUsesWith(I, I->getOperand(0));
1340 if ((DemandedMask & ~RHSKnownOne & LHSKnownZero) ==
1341 (DemandedMask & ~RHSKnownOne))
1342 return UpdateValueUsesWith(I, I->getOperand(1));
1343
1344 // If all of the potentially set bits on one side are known to be set on
1345 // the other side, just use the 'other' side.
1346 if ((DemandedMask & (~RHSKnownZero) & LHSKnownOne) ==
1347 (DemandedMask & (~RHSKnownZero)))
1348 return UpdateValueUsesWith(I, I->getOperand(0));
1349 if ((DemandedMask & (~LHSKnownZero) & RHSKnownOne) ==
1350 (DemandedMask & (~LHSKnownZero)))
1351 return UpdateValueUsesWith(I, I->getOperand(1));
1352
1353 // If the RHS is a constant, see if we can simplify it.
1354 if (ShrinkDemandedConstant(I, 1, DemandedMask))
1355 return UpdateValueUsesWith(I, I);
1356
1357 // Output known-0 bits are only known if clear in both the LHS & RHS.
1358 RHSKnownZero &= LHSKnownZero;
1359 // Output known-1 are known to be set if set in either the LHS | RHS.
1360 RHSKnownOne |= LHSKnownOne;
1361 break;
1362 case Instruction::Xor: {
1363 if (SimplifyDemandedBits(I->getOperand(1), DemandedMask,
1364 RHSKnownZero, RHSKnownOne, Depth+1))
1365 return true;
1366 assert((RHSKnownZero & RHSKnownOne) == 0 &&
1367 "Bits known to be one AND zero?");
1368 if (SimplifyDemandedBits(I->getOperand(0), DemandedMask,
1369 LHSKnownZero, LHSKnownOne, Depth+1))
1370 return true;
1371 assert((LHSKnownZero & LHSKnownOne) == 0 &&
1372 "Bits known to be one AND zero?");
1373
1374 // If all of the demanded bits are known zero on one side, return the other.
1375 // These bits cannot contribute to the result of the 'xor'.
1376 if ((DemandedMask & RHSKnownZero) == DemandedMask)
1377 return UpdateValueUsesWith(I, I->getOperand(0));
1378 if ((DemandedMask & LHSKnownZero) == DemandedMask)
1379 return UpdateValueUsesWith(I, I->getOperand(1));
1380
1381 // Output known-0 bits are known if clear or set in both the LHS & RHS.
1382 APInt KnownZeroOut = (RHSKnownZero & LHSKnownZero) |
1383 (RHSKnownOne & LHSKnownOne);
1384 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1385 APInt KnownOneOut = (RHSKnownZero & LHSKnownOne) |
1386 (RHSKnownOne & LHSKnownZero);
1387
1388 // If all of the demanded bits are known to be zero on one side or the
1389 // other, turn this into an *inclusive* or.
1390 // e.g. (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0
1391 if ((DemandedMask & ~RHSKnownZero & ~LHSKnownZero) == 0) {
1392 Instruction *Or =
1393 BinaryOperator::createOr(I->getOperand(0), I->getOperand(1),
1394 I->getName());
1395 InsertNewInstBefore(Or, *I);
1396 return UpdateValueUsesWith(I, Or);
1397 }
1398
1399 // If all of the demanded bits on one side are known, and all of the set
1400 // bits on that side are also known to be set on the other side, turn this
1401 // into an AND, as we know the bits will be cleared.
1402 // e.g. (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2
1403 if ((DemandedMask & (RHSKnownZero|RHSKnownOne)) == DemandedMask) {
1404 // all known
1405 if ((RHSKnownOne & LHSKnownOne) == RHSKnownOne) {
1406 Constant *AndC = ConstantInt::get(~RHSKnownOne & DemandedMask);
1407 Instruction *And =
1408 BinaryOperator::createAnd(I->getOperand(0), AndC, "tmp");
1409 InsertNewInstBefore(And, *I);
1410 return UpdateValueUsesWith(I, And);
1411 }
1412 }
1413
1414 // If the RHS is a constant, see if we can simplify it.
1415 // FIXME: for XOR, we prefer to force bits to 1 if they will make a -1.
1416 if (ShrinkDemandedConstant(I, 1, DemandedMask))
1417 return UpdateValueUsesWith(I, I);
1418
1419 RHSKnownZero = KnownZeroOut;
1420 RHSKnownOne = KnownOneOut;
1421 break;
1422 }
1423 case Instruction::Select:
1424 if (SimplifyDemandedBits(I->getOperand(2), DemandedMask,
1425 RHSKnownZero, RHSKnownOne, Depth+1))
1426 return true;
1427 if (SimplifyDemandedBits(I->getOperand(1), DemandedMask,
1428 LHSKnownZero, LHSKnownOne, Depth+1))
1429 return true;
1430 assert((RHSKnownZero & RHSKnownOne) == 0 &&
1431 "Bits known to be one AND zero?");
1432 assert((LHSKnownZero & LHSKnownOne) == 0 &&
1433 "Bits known to be one AND zero?");
1434
1435 // If the operands are constants, see if we can simplify them.
1436 if (ShrinkDemandedConstant(I, 1, DemandedMask))
1437 return UpdateValueUsesWith(I, I);
1438 if (ShrinkDemandedConstant(I, 2, DemandedMask))
1439 return UpdateValueUsesWith(I, I);
1440
1441 // Only known if known in both the LHS and RHS.
1442 RHSKnownOne &= LHSKnownOne;
1443 RHSKnownZero &= LHSKnownZero;
1444 break;
1445 case Instruction::Trunc: {
1446 uint32_t truncBf =
1447 cast<IntegerType>(I->getOperand(0)->getType())->getBitWidth();
Zhou Shenga4475572007-03-29 02:26:30 +00001448 DemandedMask.zext(truncBf);
1449 RHSKnownZero.zext(truncBf);
1450 RHSKnownOne.zext(truncBf);
1451 if (SimplifyDemandedBits(I->getOperand(0), DemandedMask,
1452 RHSKnownZero, RHSKnownOne, Depth+1))
Reid Spencer1791f232007-03-12 17:25:59 +00001453 return true;
1454 DemandedMask.trunc(BitWidth);
1455 RHSKnownZero.trunc(BitWidth);
1456 RHSKnownOne.trunc(BitWidth);
1457 assert((RHSKnownZero & RHSKnownOne) == 0 &&
1458 "Bits known to be one AND zero?");
1459 break;
1460 }
1461 case Instruction::BitCast:
1462 if (!I->getOperand(0)->getType()->isInteger())
1463 return false;
1464
1465 if (SimplifyDemandedBits(I->getOperand(0), DemandedMask,
1466 RHSKnownZero, RHSKnownOne, Depth+1))
1467 return true;
1468 assert((RHSKnownZero & RHSKnownOne) == 0 &&
1469 "Bits known to be one AND zero?");
1470 break;
1471 case Instruction::ZExt: {
1472 // Compute the bits in the result that are not present in the input.
1473 const IntegerType *SrcTy = cast<IntegerType>(I->getOperand(0)->getType());
Reid Spencercd99fbd2007-03-25 04:26:16 +00001474 uint32_t SrcBitWidth = SrcTy->getBitWidth();
Reid Spencer1791f232007-03-12 17:25:59 +00001475
Zhou Sheng444af492007-03-29 04:45:55 +00001476 DemandedMask.trunc(SrcBitWidth);
1477 RHSKnownZero.trunc(SrcBitWidth);
1478 RHSKnownOne.trunc(SrcBitWidth);
Zhou Shenga4475572007-03-29 02:26:30 +00001479 if (SimplifyDemandedBits(I->getOperand(0), DemandedMask,
1480 RHSKnownZero, RHSKnownOne, Depth+1))
Reid Spencer1791f232007-03-12 17:25:59 +00001481 return true;
1482 DemandedMask.zext(BitWidth);
1483 RHSKnownZero.zext(BitWidth);
1484 RHSKnownOne.zext(BitWidth);
1485 assert((RHSKnownZero & RHSKnownOne) == 0 &&
1486 "Bits known to be one AND zero?");
1487 // The top bits are known to be zero.
Zhou Shenga4475572007-03-29 02:26:30 +00001488 RHSKnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth);
Reid Spencer1791f232007-03-12 17:25:59 +00001489 break;
1490 }
1491 case Instruction::SExt: {
1492 // Compute the bits in the result that are not present in the input.
1493 const IntegerType *SrcTy = cast<IntegerType>(I->getOperand(0)->getType());
Reid Spencercd99fbd2007-03-25 04:26:16 +00001494 uint32_t SrcBitWidth = SrcTy->getBitWidth();
Reid Spencer1791f232007-03-12 17:25:59 +00001495
Reid Spencer1791f232007-03-12 17:25:59 +00001496 APInt InputDemandedBits = DemandedMask &
Zhou Shenga4475572007-03-29 02:26:30 +00001497 APInt::getLowBitsSet(BitWidth, SrcBitWidth);
Reid Spencer1791f232007-03-12 17:25:59 +00001498
Zhou Shenga4475572007-03-29 02:26:30 +00001499 APInt NewBits(APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth));
Reid Spencer1791f232007-03-12 17:25:59 +00001500 // If any of the sign extended bits are demanded, we know that the sign
1501 // bit is demanded.
1502 if ((NewBits & DemandedMask) != 0)
Zhou Sheng9bc8ab12007-04-02 13:45:30 +00001503 InputDemandedBits.set(SrcBitWidth-1);
Reid Spencer1791f232007-03-12 17:25:59 +00001504
Zhou Sheng444af492007-03-29 04:45:55 +00001505 InputDemandedBits.trunc(SrcBitWidth);
1506 RHSKnownZero.trunc(SrcBitWidth);
1507 RHSKnownOne.trunc(SrcBitWidth);
Zhou Shenga4475572007-03-29 02:26:30 +00001508 if (SimplifyDemandedBits(I->getOperand(0), InputDemandedBits,
1509 RHSKnownZero, RHSKnownOne, Depth+1))
Reid Spencer1791f232007-03-12 17:25:59 +00001510 return true;
1511 InputDemandedBits.zext(BitWidth);
1512 RHSKnownZero.zext(BitWidth);
1513 RHSKnownOne.zext(BitWidth);
1514 assert((RHSKnownZero & RHSKnownOne) == 0 &&
1515 "Bits known to be one AND zero?");
1516
1517 // If the sign bit of the input is known set or clear, then we know the
1518 // top bits of the result.
1519
1520 // If the input sign bit is known zero, or if the NewBits are not demanded
1521 // convert this into a zero extension.
Zhou Shenga4475572007-03-29 02:26:30 +00001522 if (RHSKnownZero[SrcBitWidth-1] || (NewBits & ~DemandedMask) == NewBits)
Reid Spencer1791f232007-03-12 17:25:59 +00001523 {
1524 // Convert to ZExt cast
1525 CastInst *NewCast = new ZExtInst(I->getOperand(0), VTy, I->getName(), I);
1526 return UpdateValueUsesWith(I, NewCast);
Zhou Shenga4475572007-03-29 02:26:30 +00001527 } else if (RHSKnownOne[SrcBitWidth-1]) { // Input sign bit known set
Reid Spencer1791f232007-03-12 17:25:59 +00001528 RHSKnownOne |= NewBits;
Reid Spencer1791f232007-03-12 17:25:59 +00001529 }
1530 break;
1531 }
1532 case Instruction::Add: {
1533 // Figure out what the input bits are. If the top bits of the and result
1534 // are not demanded, then the add doesn't demand them from its input
1535 // either.
Reid Spencer52830322007-03-25 21:11:44 +00001536 uint32_t NLZ = DemandedMask.countLeadingZeros();
Reid Spencer1791f232007-03-12 17:25:59 +00001537
1538 // If there is a constant on the RHS, there are a variety of xformations
1539 // we can do.
1540 if (ConstantInt *RHS = dyn_cast<ConstantInt>(I->getOperand(1))) {
1541 // If null, this should be simplified elsewhere. Some of the xforms here
1542 // won't work if the RHS is zero.
1543 if (RHS->isZero())
1544 break;
1545
1546 // If the top bit of the output is demanded, demand everything from the
1547 // input. Otherwise, we demand all the input bits except NLZ top bits.
Zhou Shenga4475572007-03-29 02:26:30 +00001548 APInt InDemandedBits(APInt::getLowBitsSet(BitWidth, BitWidth - NLZ));
Reid Spencer1791f232007-03-12 17:25:59 +00001549
1550 // Find information about known zero/one bits in the input.
1551 if (SimplifyDemandedBits(I->getOperand(0), InDemandedBits,
1552 LHSKnownZero, LHSKnownOne, Depth+1))
1553 return true;
1554
1555 // If the RHS of the add has bits set that can't affect the input, reduce
1556 // the constant.
1557 if (ShrinkDemandedConstant(I, 1, InDemandedBits))
1558 return UpdateValueUsesWith(I, I);
1559
1560 // Avoid excess work.
1561 if (LHSKnownZero == 0 && LHSKnownOne == 0)
1562 break;
1563
1564 // Turn it into OR if input bits are zero.
1565 if ((LHSKnownZero & RHS->getValue()) == RHS->getValue()) {
1566 Instruction *Or =
1567 BinaryOperator::createOr(I->getOperand(0), I->getOperand(1),
1568 I->getName());
1569 InsertNewInstBefore(Or, *I);
1570 return UpdateValueUsesWith(I, Or);
1571 }
1572
1573 // We can say something about the output known-zero and known-one bits,
1574 // depending on potential carries from the input constant and the
1575 // unknowns. For example if the LHS is known to have at most the 0x0F0F0
1576 // bits set and the RHS constant is 0x01001, then we know we have a known
1577 // one mask of 0x00001 and a known zero mask of 0xE0F0E.
1578
1579 // To compute this, we first compute the potential carry bits. These are
1580 // the bits which may be modified. I'm not aware of a better way to do
1581 // this scan.
Zhou Sheng4f164022007-03-31 02:38:39 +00001582 const APInt& RHSVal = RHS->getValue();
1583 APInt CarryBits((~LHSKnownZero + RHSVal) ^ (~LHSKnownZero ^ RHSVal));
Reid Spencer1791f232007-03-12 17:25:59 +00001584
1585 // Now that we know which bits have carries, compute the known-1/0 sets.
1586
1587 // Bits are known one if they are known zero in one operand and one in the
1588 // other, and there is no input carry.
1589 RHSKnownOne = ((LHSKnownZero & RHSVal) |
1590 (LHSKnownOne & ~RHSVal)) & ~CarryBits;
1591
1592 // Bits are known zero if they are known zero in both operands and there
1593 // is no input carry.
1594 RHSKnownZero = LHSKnownZero & ~RHSVal & ~CarryBits;
1595 } else {
1596 // If the high-bits of this ADD are not demanded, then it does not demand
1597 // the high bits of its LHS or RHS.
Zhou Shenga4475572007-03-29 02:26:30 +00001598 if (DemandedMask[BitWidth-1] == 0) {
Reid Spencer1791f232007-03-12 17:25:59 +00001599 // Right fill the mask of bits for this ADD to demand the most
1600 // significant bit and all those below it.
Zhou Shenga4475572007-03-29 02:26:30 +00001601 APInt DemandedFromOps(APInt::getLowBitsSet(BitWidth, BitWidth-NLZ));
Reid Spencer1791f232007-03-12 17:25:59 +00001602 if (SimplifyDemandedBits(I->getOperand(0), DemandedFromOps,
1603 LHSKnownZero, LHSKnownOne, Depth+1))
1604 return true;
1605 if (SimplifyDemandedBits(I->getOperand(1), DemandedFromOps,
1606 LHSKnownZero, LHSKnownOne, Depth+1))
1607 return true;
1608 }
1609 }
1610 break;
1611 }
1612 case Instruction::Sub:
1613 // If the high-bits of this SUB are not demanded, then it does not demand
1614 // the high bits of its LHS or RHS.
Zhou Shenga4475572007-03-29 02:26:30 +00001615 if (DemandedMask[BitWidth-1] == 0) {
Reid Spencer1791f232007-03-12 17:25:59 +00001616 // Right fill the mask of bits for this SUB to demand the most
1617 // significant bit and all those below it.
Zhou Sheng56cda952007-04-02 08:20:41 +00001618 uint32_t NLZ = DemandedMask.countLeadingZeros();
Zhou Shenga4475572007-03-29 02:26:30 +00001619 APInt DemandedFromOps(APInt::getLowBitsSet(BitWidth, BitWidth-NLZ));
Reid Spencer1791f232007-03-12 17:25:59 +00001620 if (SimplifyDemandedBits(I->getOperand(0), DemandedFromOps,
1621 LHSKnownZero, LHSKnownOne, Depth+1))
1622 return true;
1623 if (SimplifyDemandedBits(I->getOperand(1), DemandedFromOps,
1624 LHSKnownZero, LHSKnownOne, Depth+1))
1625 return true;
1626 }
Dan Gohman72ec3f42008-04-28 17:02:21 +00001627 // Otherwise just hand the sub off to ComputeMaskedBits to fill in
1628 // the known zeros and ones.
1629 ComputeMaskedBits(V, DemandedMask, RHSKnownZero, RHSKnownOne, Depth);
Reid Spencer1791f232007-03-12 17:25:59 +00001630 break;
1631 case Instruction::Shl:
1632 if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
Zhou Shengb25806f2007-03-30 09:29:48 +00001633 uint64_t ShiftAmt = SA->getLimitedValue(BitWidth);
Zhou Shenga4475572007-03-29 02:26:30 +00001634 APInt DemandedMaskIn(DemandedMask.lshr(ShiftAmt));
1635 if (SimplifyDemandedBits(I->getOperand(0), DemandedMaskIn,
Reid Spencer1791f232007-03-12 17:25:59 +00001636 RHSKnownZero, RHSKnownOne, Depth+1))
1637 return true;
1638 assert((RHSKnownZero & RHSKnownOne) == 0 &&
1639 "Bits known to be one AND zero?");
1640 RHSKnownZero <<= ShiftAmt;
1641 RHSKnownOne <<= ShiftAmt;
1642 // low bits known zero.
Zhou Shengd8c645b2007-03-14 09:07:33 +00001643 if (ShiftAmt)
Zhou Sheng23f7a1c2007-03-28 15:02:20 +00001644 RHSKnownZero |= APInt::getLowBitsSet(BitWidth, ShiftAmt);
Reid Spencer1791f232007-03-12 17:25:59 +00001645 }
1646 break;
1647 case Instruction::LShr:
1648 // For a logical shift right
1649 if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
Zhou Shengb25806f2007-03-30 09:29:48 +00001650 uint64_t ShiftAmt = SA->getLimitedValue(BitWidth);
Reid Spencer1791f232007-03-12 17:25:59 +00001651
Reid Spencer1791f232007-03-12 17:25:59 +00001652 // Unsigned shift right.
Zhou Shenga4475572007-03-29 02:26:30 +00001653 APInt DemandedMaskIn(DemandedMask.shl(ShiftAmt));
1654 if (SimplifyDemandedBits(I->getOperand(0), DemandedMaskIn,
Reid Spencer1791f232007-03-12 17:25:59 +00001655 RHSKnownZero, RHSKnownOne, Depth+1))
1656 return true;
1657 assert((RHSKnownZero & RHSKnownOne) == 0 &&
1658 "Bits known to be one AND zero?");
Reid Spencer1791f232007-03-12 17:25:59 +00001659 RHSKnownZero = APIntOps::lshr(RHSKnownZero, ShiftAmt);
1660 RHSKnownOne = APIntOps::lshr(RHSKnownOne, ShiftAmt);
Zhou Shengd8c645b2007-03-14 09:07:33 +00001661 if (ShiftAmt) {
1662 // Compute the new bits that are at the top now.
Zhou Shenga4475572007-03-29 02:26:30 +00001663 APInt HighBits(APInt::getHighBitsSet(BitWidth, ShiftAmt));
Zhou Shengd8c645b2007-03-14 09:07:33 +00001664 RHSKnownZero |= HighBits; // high bits known zero.
1665 }
Reid Spencer1791f232007-03-12 17:25:59 +00001666 }
1667 break;
1668 case Instruction::AShr:
1669 // If this is an arithmetic shift right and only the low-bit is set, we can
1670 // always convert this into a logical shr, even if the shift amount is
1671 // variable. The low bit of the shift cannot be an input sign bit unless
1672 // the shift amount is >= the size of the datatype, which is undefined.
1673 if (DemandedMask == 1) {
1674 // Perform the logical shift right.
1675 Value *NewVal = BinaryOperator::createLShr(
1676 I->getOperand(0), I->getOperand(1), I->getName());
1677 InsertNewInstBefore(cast<Instruction>(NewVal), *I);
1678 return UpdateValueUsesWith(I, NewVal);
1679 }
Chris Lattnerd4fef8d2007-07-15 20:54:51 +00001680
1681 // If the sign bit is the only bit demanded by this ashr, then there is no
1682 // need to do it, the shift doesn't change the high bit.
1683 if (DemandedMask.isSignBit())
1684 return UpdateValueUsesWith(I, I->getOperand(0));
Reid Spencer1791f232007-03-12 17:25:59 +00001685
1686 if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
Zhou Shengfd28a332007-03-30 17:20:39 +00001687 uint32_t ShiftAmt = SA->getLimitedValue(BitWidth);
Reid Spencer1791f232007-03-12 17:25:59 +00001688
Reid Spencer1791f232007-03-12 17:25:59 +00001689 // Signed shift right.
Zhou Shenga4475572007-03-29 02:26:30 +00001690 APInt DemandedMaskIn(DemandedMask.shl(ShiftAmt));
Lauro Ramos Venancio368e8872007-06-06 17:08:48 +00001691 // If any of the "high bits" are demanded, we should set the sign bit as
1692 // demanded.
1693 if (DemandedMask.countLeadingZeros() <= ShiftAmt)
1694 DemandedMaskIn.set(BitWidth-1);
Reid Spencer1791f232007-03-12 17:25:59 +00001695 if (SimplifyDemandedBits(I->getOperand(0),
Zhou Shenga4475572007-03-29 02:26:30 +00001696 DemandedMaskIn,
Reid Spencer1791f232007-03-12 17:25:59 +00001697 RHSKnownZero, RHSKnownOne, Depth+1))
1698 return true;
1699 assert((RHSKnownZero & RHSKnownOne) == 0 &&
1700 "Bits known to be one AND zero?");
1701 // Compute the new bits that are at the top now.
Zhou Shenga4475572007-03-29 02:26:30 +00001702 APInt HighBits(APInt::getHighBitsSet(BitWidth, ShiftAmt));
Reid Spencer1791f232007-03-12 17:25:59 +00001703 RHSKnownZero = APIntOps::lshr(RHSKnownZero, ShiftAmt);
1704 RHSKnownOne = APIntOps::lshr(RHSKnownOne, ShiftAmt);
1705
1706 // Handle the sign bits.
1707 APInt SignBit(APInt::getSignBit(BitWidth));
1708 // Adjust to where it is now in the mask.
1709 SignBit = APIntOps::lshr(SignBit, ShiftAmt);
1710
1711 // If the input sign bit is known to be zero, or if none of the top bits
1712 // are demanded, turn this into an unsigned shift right.
Zhou Shenga4475572007-03-29 02:26:30 +00001713 if (RHSKnownZero[BitWidth-ShiftAmt-1] ||
Reid Spencer1791f232007-03-12 17:25:59 +00001714 (HighBits & ~DemandedMask) == HighBits) {
1715 // Perform the logical shift right.
1716 Value *NewVal = BinaryOperator::createLShr(
1717 I->getOperand(0), SA, I->getName());
1718 InsertNewInstBefore(cast<Instruction>(NewVal), *I);
1719 return UpdateValueUsesWith(I, NewVal);
1720 } else if ((RHSKnownOne & SignBit) != 0) { // New bits are known one.
1721 RHSKnownOne |= HighBits;
1722 }
1723 }
1724 break;
Nick Lewyckyd0b62a12008-03-06 06:48:30 +00001725 case Instruction::SRem:
1726 if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) {
1727 APInt RA = Rem->getValue();
1728 if (RA.isPowerOf2() || (-RA).isPowerOf2()) {
1729 APInt LowBits = RA.isStrictlyPositive() ? (RA - 1) | RA : ~RA;
1730 APInt Mask2 = LowBits | APInt::getSignBit(BitWidth);
1731 if (SimplifyDemandedBits(I->getOperand(0), Mask2,
1732 LHSKnownZero, LHSKnownOne, Depth+1))
1733 return true;
1734
1735 if (LHSKnownZero[BitWidth-1] || ((LHSKnownZero & LowBits) == LowBits))
1736 LHSKnownZero |= ~LowBits;
1737 else if (LHSKnownOne[BitWidth-1])
1738 LHSKnownOne |= ~LowBits;
1739
1740 KnownZero |= LHSKnownZero & DemandedMask;
1741 KnownOne |= LHSKnownOne & DemandedMask;
1742
1743 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
1744 }
1745 }
1746 break;
Dan Gohman72ec3f42008-04-28 17:02:21 +00001747 case Instruction::URem: {
Nick Lewyckyd0b62a12008-03-06 06:48:30 +00001748 if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) {
1749 APInt RA = Rem->getValue();
Dan Gohman72ec3f42008-04-28 17:02:21 +00001750 if (RA.isStrictlyPositive() && RA.isPowerOf2()) {
Nick Lewyckyd0b62a12008-03-06 06:48:30 +00001751 APInt LowBits = (RA - 1) | RA;
1752 APInt Mask2 = LowBits & DemandedMask;
1753 KnownZero |= ~LowBits & DemandedMask;
1754 if (SimplifyDemandedBits(I->getOperand(0), Mask2,
1755 KnownZero, KnownOne, Depth+1))
1756 return true;
1757
1758 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
Dan Gohman72ec3f42008-04-28 17:02:21 +00001759 break;
Nick Lewyckyd0b62a12008-03-06 06:48:30 +00001760 }
Nick Lewyckyd0b62a12008-03-06 06:48:30 +00001761 }
Dan Gohman72ec3f42008-04-28 17:02:21 +00001762
1763 APInt KnownZero2(BitWidth, 0), KnownOne2(BitWidth, 0);
1764 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
1765 ComputeMaskedBits(I->getOperand(0), AllOnes,
1766 KnownZero2, KnownOne2, Depth+1);
1767 uint32_t Leaders = KnownZero2.countLeadingOnes();
1768 APInt HighZeros = APInt::getHighBitsSet(BitWidth, Leaders);
1769 if (SimplifyDemandedBits(I->getOperand(1), ~HighZeros,
1770 KnownZero2, KnownOne2, Depth+1))
1771 return true;
1772
1773 Leaders = std::max(Leaders,
1774 KnownZero2.countLeadingOnes());
1775 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders) & DemandedMask;
Nick Lewyckyd0b62a12008-03-06 06:48:30 +00001776 break;
Reid Spencer1791f232007-03-12 17:25:59 +00001777 }
Dan Gohman72ec3f42008-04-28 17:02:21 +00001778 }
Reid Spencer1791f232007-03-12 17:25:59 +00001779
1780 // If the client is only demanding bits that we know, return the known
1781 // constant.
1782 if ((DemandedMask & (RHSKnownZero|RHSKnownOne)) == DemandedMask)
1783 return UpdateValueUsesWith(I, ConstantInt::get(RHSKnownOne));
1784 return false;
1785}
1786
Chris Lattner2deeaea2006-10-05 06:55:50 +00001787
1788/// SimplifyDemandedVectorElts - The specified value producecs a vector with
1789/// 64 or fewer elements. DemandedElts contains the set of elements that are
1790/// actually used by the caller. This method analyzes which elements of the
1791/// operand are undef and returns that information in UndefElts.
1792///
1793/// If the information about demanded elements can be used to simplify the
1794/// operation, the operation is simplified, then the resultant value is
1795/// returned. This returns null if no change was made.
1796Value *InstCombiner::SimplifyDemandedVectorElts(Value *V, uint64_t DemandedElts,
1797 uint64_t &UndefElts,
1798 unsigned Depth) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001799 unsigned VWidth = cast<VectorType>(V->getType())->getNumElements();
Chris Lattner2deeaea2006-10-05 06:55:50 +00001800 assert(VWidth <= 64 && "Vector too wide to analyze!");
1801 uint64_t EltMask = ~0ULL >> (64-VWidth);
1802 assert(DemandedElts != EltMask && (DemandedElts & ~EltMask) == 0 &&
1803 "Invalid DemandedElts!");
1804
1805 if (isa<UndefValue>(V)) {
1806 // If the entire vector is undefined, just return this info.
1807 UndefElts = EltMask;
1808 return 0;
1809 } else if (DemandedElts == 0) { // If nothing is demanded, provide undef.
1810 UndefElts = EltMask;
1811 return UndefValue::get(V->getType());
1812 }
1813
1814 UndefElts = 0;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001815 if (ConstantVector *CP = dyn_cast<ConstantVector>(V)) {
1816 const Type *EltTy = cast<VectorType>(V->getType())->getElementType();
Chris Lattner2deeaea2006-10-05 06:55:50 +00001817 Constant *Undef = UndefValue::get(EltTy);
1818
1819 std::vector<Constant*> Elts;
1820 for (unsigned i = 0; i != VWidth; ++i)
1821 if (!(DemandedElts & (1ULL << i))) { // If not demanded, set to undef.
1822 Elts.push_back(Undef);
1823 UndefElts |= (1ULL << i);
1824 } else if (isa<UndefValue>(CP->getOperand(i))) { // Already undef.
1825 Elts.push_back(Undef);
1826 UndefElts |= (1ULL << i);
1827 } else { // Otherwise, defined.
1828 Elts.push_back(CP->getOperand(i));
1829 }
1830
1831 // If we changed the constant, return it.
Reid Spencerd84d35b2007-02-15 02:26:10 +00001832 Constant *NewCP = ConstantVector::get(Elts);
Chris Lattner2deeaea2006-10-05 06:55:50 +00001833 return NewCP != CP ? NewCP : 0;
1834 } else if (isa<ConstantAggregateZero>(V)) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001835 // Simplify the CAZ to a ConstantVector where the non-demanded elements are
Chris Lattner2deeaea2006-10-05 06:55:50 +00001836 // set to undef.
Reid Spencerd84d35b2007-02-15 02:26:10 +00001837 const Type *EltTy = cast<VectorType>(V->getType())->getElementType();
Chris Lattner2deeaea2006-10-05 06:55:50 +00001838 Constant *Zero = Constant::getNullValue(EltTy);
1839 Constant *Undef = UndefValue::get(EltTy);
1840 std::vector<Constant*> Elts;
1841 for (unsigned i = 0; i != VWidth; ++i)
1842 Elts.push_back((DemandedElts & (1ULL << i)) ? Zero : Undef);
1843 UndefElts = DemandedElts ^ EltMask;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001844 return ConstantVector::get(Elts);
Chris Lattner2deeaea2006-10-05 06:55:50 +00001845 }
1846
1847 if (!V->hasOneUse()) { // Other users may use these bits.
1848 if (Depth != 0) { // Not at the root.
1849 // TODO: Just compute the UndefElts information recursively.
1850 return false;
1851 }
1852 return false;
1853 } else if (Depth == 10) { // Limit search depth.
1854 return false;
1855 }
1856
1857 Instruction *I = dyn_cast<Instruction>(V);
1858 if (!I) return false; // Only analyze instructions.
1859
1860 bool MadeChange = false;
1861 uint64_t UndefElts2;
1862 Value *TmpV;
1863 switch (I->getOpcode()) {
1864 default: break;
1865
1866 case Instruction::InsertElement: {
1867 // If this is a variable index, we don't know which element it overwrites.
1868 // demand exactly the same input as we produce.
Reid Spencere0fc4df2006-10-20 07:07:24 +00001869 ConstantInt *Idx = dyn_cast<ConstantInt>(I->getOperand(2));
Chris Lattner2deeaea2006-10-05 06:55:50 +00001870 if (Idx == 0) {
1871 // Note that we can't propagate undef elt info, because we don't know
1872 // which elt is getting updated.
1873 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts,
1874 UndefElts2, Depth+1);
1875 if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1876 break;
1877 }
1878
1879 // If this is inserting an element that isn't demanded, remove this
1880 // insertelement.
Reid Spencere0fc4df2006-10-20 07:07:24 +00001881 unsigned IdxNo = Idx->getZExtValue();
Chris Lattner2deeaea2006-10-05 06:55:50 +00001882 if (IdxNo >= VWidth || (DemandedElts & (1ULL << IdxNo)) == 0)
1883 return AddSoonDeadInstToWorklist(*I, 0);
1884
1885 // Otherwise, the element inserted overwrites whatever was there, so the
1886 // input demanded set is simpler than the output set.
1887 TmpV = SimplifyDemandedVectorElts(I->getOperand(0),
1888 DemandedElts & ~(1ULL << IdxNo),
1889 UndefElts, Depth+1);
1890 if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1891
1892 // The inserted element is defined.
1893 UndefElts |= 1ULL << IdxNo;
1894 break;
1895 }
Chris Lattnerb37fb6a2007-04-14 22:29:23 +00001896 case Instruction::BitCast: {
Dan Gohman06c60b62007-07-16 14:29:03 +00001897 // Vector->vector casts only.
Chris Lattnerb37fb6a2007-04-14 22:29:23 +00001898 const VectorType *VTy = dyn_cast<VectorType>(I->getOperand(0)->getType());
1899 if (!VTy) break;
1900 unsigned InVWidth = VTy->getNumElements();
1901 uint64_t InputDemandedElts = 0;
1902 unsigned Ratio;
1903
1904 if (VWidth == InVWidth) {
Dan Gohman06c60b62007-07-16 14:29:03 +00001905 // If we are converting from <4 x i32> -> <4 x f32>, we demand the same
Chris Lattnerb37fb6a2007-04-14 22:29:23 +00001906 // elements as are demanded of us.
1907 Ratio = 1;
1908 InputDemandedElts = DemandedElts;
1909 } else if (VWidth > InVWidth) {
1910 // Untested so far.
1911 break;
1912
1913 // If there are more elements in the result than there are in the source,
1914 // then an input element is live if any of the corresponding output
1915 // elements are live.
1916 Ratio = VWidth/InVWidth;
1917 for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx) {
1918 if (DemandedElts & (1ULL << OutIdx))
1919 InputDemandedElts |= 1ULL << (OutIdx/Ratio);
1920 }
1921 } else {
1922 // Untested so far.
1923 break;
1924
1925 // If there are more elements in the source than there are in the result,
1926 // then an input element is live if the corresponding output element is
1927 // live.
1928 Ratio = InVWidth/VWidth;
1929 for (unsigned InIdx = 0; InIdx != InVWidth; ++InIdx)
1930 if (DemandedElts & (1ULL << InIdx/Ratio))
1931 InputDemandedElts |= 1ULL << InIdx;
1932 }
Chris Lattner2deeaea2006-10-05 06:55:50 +00001933
Chris Lattnerb37fb6a2007-04-14 22:29:23 +00001934 // div/rem demand all inputs, because they don't want divide by zero.
1935 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), InputDemandedElts,
1936 UndefElts2, Depth+1);
1937 if (TmpV) {
1938 I->setOperand(0, TmpV);
1939 MadeChange = true;
1940 }
1941
1942 UndefElts = UndefElts2;
1943 if (VWidth > InVWidth) {
1944 assert(0 && "Unimp");
1945 // If there are more elements in the result than there are in the source,
1946 // then an output element is undef if the corresponding input element is
1947 // undef.
1948 for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx)
1949 if (UndefElts2 & (1ULL << (OutIdx/Ratio)))
1950 UndefElts |= 1ULL << OutIdx;
1951 } else if (VWidth < InVWidth) {
1952 assert(0 && "Unimp");
1953 // If there are more elements in the source than there are in the result,
1954 // then a result element is undef if all of the corresponding input
1955 // elements are undef.
1956 UndefElts = ~0ULL >> (64-VWidth); // Start out all undef.
1957 for (unsigned InIdx = 0; InIdx != InVWidth; ++InIdx)
1958 if ((UndefElts2 & (1ULL << InIdx)) == 0) // Not undef?
1959 UndefElts &= ~(1ULL << (InIdx/Ratio)); // Clear undef bit.
1960 }
1961 break;
1962 }
Chris Lattner2deeaea2006-10-05 06:55:50 +00001963 case Instruction::And:
1964 case Instruction::Or:
1965 case Instruction::Xor:
1966 case Instruction::Add:
1967 case Instruction::Sub:
1968 case Instruction::Mul:
1969 // div/rem demand all inputs, because they don't want divide by zero.
1970 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts,
1971 UndefElts, Depth+1);
1972 if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1973 TmpV = SimplifyDemandedVectorElts(I->getOperand(1), DemandedElts,
1974 UndefElts2, Depth+1);
1975 if (TmpV) { I->setOperand(1, TmpV); MadeChange = true; }
1976
1977 // Output elements are undefined if both are undefined. Consider things
1978 // like undef&0. The result is known zero, not undef.
1979 UndefElts &= UndefElts2;
1980 break;
1981
1982 case Instruction::Call: {
1983 IntrinsicInst *II = dyn_cast<IntrinsicInst>(I);
1984 if (!II) break;
1985 switch (II->getIntrinsicID()) {
1986 default: break;
1987
1988 // Binary vector operations that work column-wise. A dest element is a
1989 // function of the corresponding input elements from the two inputs.
1990 case Intrinsic::x86_sse_sub_ss:
1991 case Intrinsic::x86_sse_mul_ss:
1992 case Intrinsic::x86_sse_min_ss:
1993 case Intrinsic::x86_sse_max_ss:
1994 case Intrinsic::x86_sse2_sub_sd:
1995 case Intrinsic::x86_sse2_mul_sd:
1996 case Intrinsic::x86_sse2_min_sd:
1997 case Intrinsic::x86_sse2_max_sd:
1998 TmpV = SimplifyDemandedVectorElts(II->getOperand(1), DemandedElts,
1999 UndefElts, Depth+1);
2000 if (TmpV) { II->setOperand(1, TmpV); MadeChange = true; }
2001 TmpV = SimplifyDemandedVectorElts(II->getOperand(2), DemandedElts,
2002 UndefElts2, Depth+1);
2003 if (TmpV) { II->setOperand(2, TmpV); MadeChange = true; }
2004
2005 // If only the low elt is demanded and this is a scalarizable intrinsic,
2006 // scalarize it now.
2007 if (DemandedElts == 1) {
2008 switch (II->getIntrinsicID()) {
2009 default: break;
2010 case Intrinsic::x86_sse_sub_ss:
2011 case Intrinsic::x86_sse_mul_ss:
2012 case Intrinsic::x86_sse2_sub_sd:
2013 case Intrinsic::x86_sse2_mul_sd:
2014 // TODO: Lower MIN/MAX/ABS/etc
2015 Value *LHS = II->getOperand(1);
2016 Value *RHS = II->getOperand(2);
2017 // Extract the element as scalars.
2018 LHS = InsertNewInstBefore(new ExtractElementInst(LHS, 0U,"tmp"), *II);
2019 RHS = InsertNewInstBefore(new ExtractElementInst(RHS, 0U,"tmp"), *II);
2020
2021 switch (II->getIntrinsicID()) {
2022 default: assert(0 && "Case stmts out of sync!");
2023 case Intrinsic::x86_sse_sub_ss:
2024 case Intrinsic::x86_sse2_sub_sd:
2025 TmpV = InsertNewInstBefore(BinaryOperator::createSub(LHS, RHS,
2026 II->getName()), *II);
2027 break;
2028 case Intrinsic::x86_sse_mul_ss:
2029 case Intrinsic::x86_sse2_mul_sd:
2030 TmpV = InsertNewInstBefore(BinaryOperator::createMul(LHS, RHS,
2031 II->getName()), *II);
2032 break;
2033 }
2034
2035 Instruction *New =
Gabor Greife9ecc682008-04-06 20:25:17 +00002036 InsertElementInst::Create(UndefValue::get(II->getType()), TmpV, 0U,
2037 II->getName());
Chris Lattner2deeaea2006-10-05 06:55:50 +00002038 InsertNewInstBefore(New, *II);
2039 AddSoonDeadInstToWorklist(*II, 0);
2040 return New;
2041 }
2042 }
2043
2044 // Output elements are undefined if both are undefined. Consider things
2045 // like undef&0. The result is known zero, not undef.
2046 UndefElts &= UndefElts2;
2047 break;
2048 }
2049 break;
2050 }
2051 }
2052 return MadeChange ? I : 0;
2053}
2054
Nick Lewycky0c5c4792007-09-06 02:40:25 +00002055/// @returns true if the specified compare predicate is
Reid Spencer266e42b2006-12-23 06:05:41 +00002056/// true when both operands are equal...
Nick Lewycky0c5c4792007-09-06 02:40:25 +00002057/// @brief Determine if the icmp Predicate is true when both operands are equal
2058static bool isTrueWhenEqual(ICmpInst::Predicate pred) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002059 return pred == ICmpInst::ICMP_EQ || pred == ICmpInst::ICMP_UGE ||
2060 pred == ICmpInst::ICMP_SGE || pred == ICmpInst::ICMP_ULE ||
2061 pred == ICmpInst::ICMP_SLE;
2062}
2063
Nick Lewycky0c5c4792007-09-06 02:40:25 +00002064/// @returns true if the specified compare instruction is
2065/// true when both operands are equal...
2066/// @brief Determine if the ICmpInst returns true when both operands are equal
2067static bool isTrueWhenEqual(ICmpInst &ICI) {
2068 return isTrueWhenEqual(ICI.getPredicate());
2069}
2070
Chris Lattnerb8b97502003-08-13 19:01:45 +00002071/// AssociativeOpt - Perform an optimization on an associative operator. This
2072/// function is designed to check a chain of associative operators for a
2073/// potential to apply a certain optimization. Since the optimization may be
2074/// applicable if the expression was reassociated, this checks the chain, then
2075/// reassociates the expression as necessary to expose the optimization
2076/// opportunity. This makes use of a special Functor, which must define
2077/// 'shouldApply' and 'apply' methods.
2078///
2079template<typename Functor>
2080Instruction *AssociativeOpt(BinaryOperator &Root, const Functor &F) {
2081 unsigned Opcode = Root.getOpcode();
2082 Value *LHS = Root.getOperand(0);
2083
2084 // Quick check, see if the immediate LHS matches...
2085 if (F.shouldApply(LHS))
2086 return F.apply(Root);
2087
2088 // Otherwise, if the LHS is not of the same opcode as the root, return.
2089 Instruction *LHSI = dyn_cast<Instruction>(LHS);
Chris Lattnerf95d9b92003-10-15 16:48:29 +00002090 while (LHSI && LHSI->getOpcode() == Opcode && LHSI->hasOneUse()) {
Chris Lattnerb8b97502003-08-13 19:01:45 +00002091 // Should we apply this transform to the RHS?
2092 bool ShouldApply = F.shouldApply(LHSI->getOperand(1));
2093
2094 // If not to the RHS, check to see if we should apply to the LHS...
2095 if (!ShouldApply && F.shouldApply(LHSI->getOperand(0))) {
2096 cast<BinaryOperator>(LHSI)->swapOperands(); // Make the LHS the RHS
2097 ShouldApply = true;
2098 }
2099
2100 // If the functor wants to apply the optimization to the RHS of LHSI,
2101 // reassociate the expression from ((? op A) op B) to (? op (A op B))
2102 if (ShouldApply) {
2103 BasicBlock *BB = Root.getParent();
Misha Brukmanb1c93172005-04-21 23:48:37 +00002104
Chris Lattnerb8b97502003-08-13 19:01:45 +00002105 // Now all of the instructions are in the current basic block, go ahead
2106 // and perform the reassociation.
2107 Instruction *TmpLHSI = cast<Instruction>(Root.getOperand(0));
2108
2109 // First move the selected RHS to the LHS of the root...
2110 Root.setOperand(0, LHSI->getOperand(1));
2111
2112 // Make what used to be the LHS of the root be the user of the root...
2113 Value *ExtraOperand = TmpLHSI->getOperand(1);
Chris Lattner284d3b02004-04-16 18:08:07 +00002114 if (&Root == TmpLHSI) {
Chris Lattner8953b902004-04-05 02:10:19 +00002115 Root.replaceAllUsesWith(Constant::getNullValue(TmpLHSI->getType()));
2116 return 0;
2117 }
Chris Lattner284d3b02004-04-16 18:08:07 +00002118 Root.replaceAllUsesWith(TmpLHSI); // Users now use TmpLHSI
Chris Lattnerb8b97502003-08-13 19:01:45 +00002119 TmpLHSI->setOperand(1, &Root); // TmpLHSI now uses the root
Chris Lattner284d3b02004-04-16 18:08:07 +00002120 TmpLHSI->getParent()->getInstList().remove(TmpLHSI);
2121 BasicBlock::iterator ARI = &Root; ++ARI;
2122 BB->getInstList().insert(ARI, TmpLHSI); // Move TmpLHSI to after Root
2123 ARI = Root;
Chris Lattnerb8b97502003-08-13 19:01:45 +00002124
2125 // Now propagate the ExtraOperand down the chain of instructions until we
2126 // get to LHSI.
2127 while (TmpLHSI != LHSI) {
2128 Instruction *NextLHSI = cast<Instruction>(TmpLHSI->getOperand(0));
Chris Lattner284d3b02004-04-16 18:08:07 +00002129 // Move the instruction to immediately before the chain we are
2130 // constructing to avoid breaking dominance properties.
2131 NextLHSI->getParent()->getInstList().remove(NextLHSI);
2132 BB->getInstList().insert(ARI, NextLHSI);
2133 ARI = NextLHSI;
2134
Chris Lattnerb8b97502003-08-13 19:01:45 +00002135 Value *NextOp = NextLHSI->getOperand(1);
2136 NextLHSI->setOperand(1, ExtraOperand);
2137 TmpLHSI = NextLHSI;
2138 ExtraOperand = NextOp;
2139 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00002140
Chris Lattnerb8b97502003-08-13 19:01:45 +00002141 // Now that the instructions are reassociated, have the functor perform
2142 // the transformation...
2143 return F.apply(Root);
2144 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00002145
Chris Lattnerb8b97502003-08-13 19:01:45 +00002146 LHSI = dyn_cast<Instruction>(LHSI->getOperand(0));
2147 }
2148 return 0;
2149}
2150
2151
2152// AddRHS - Implements: X + X --> X << 1
2153struct AddRHS {
2154 Value *RHS;
2155 AddRHS(Value *rhs) : RHS(rhs) {}
2156 bool shouldApply(Value *LHS) const { return LHS == RHS; }
2157 Instruction *apply(BinaryOperator &Add) const {
Reid Spencer0d5f9232007-02-02 14:08:20 +00002158 return BinaryOperator::createShl(Add.getOperand(0),
Reid Spencer2341c222007-02-02 02:16:23 +00002159 ConstantInt::get(Add.getType(), 1));
Chris Lattnerb8b97502003-08-13 19:01:45 +00002160 }
2161};
2162
2163// AddMaskingAnd - Implements (A & C1)+(B & C2) --> (A & C1)|(B & C2)
2164// iff C1&C2 == 0
2165struct AddMaskingAnd {
2166 Constant *C2;
2167 AddMaskingAnd(Constant *c) : C2(c) {}
2168 bool shouldApply(Value *LHS) const {
Chris Lattnerd4252a72004-07-30 07:50:03 +00002169 ConstantInt *C1;
Misha Brukmanb1c93172005-04-21 23:48:37 +00002170 return match(LHS, m_And(m_Value(), m_ConstantInt(C1))) &&
Chris Lattnerd4252a72004-07-30 07:50:03 +00002171 ConstantExpr::getAnd(C1, C2)->isNullValue();
Chris Lattnerb8b97502003-08-13 19:01:45 +00002172 }
2173 Instruction *apply(BinaryOperator &Add) const {
Chris Lattnerdf20a4d2004-06-10 02:07:29 +00002174 return BinaryOperator::createOr(Add.getOperand(0), Add.getOperand(1));
Chris Lattnerb8b97502003-08-13 19:01:45 +00002175 }
2176};
2177
Chris Lattner86102b82005-01-01 16:22:27 +00002178static Value *FoldOperationIntoSelectOperand(Instruction &I, Value *SO,
Chris Lattner183b3362004-04-09 19:05:30 +00002179 InstCombiner *IC) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002180 if (CastInst *CI = dyn_cast<CastInst>(&I)) {
Chris Lattner86102b82005-01-01 16:22:27 +00002181 if (Constant *SOC = dyn_cast<Constant>(SO))
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002182 return ConstantExpr::getCast(CI->getOpcode(), SOC, I.getType());
Misha Brukmanb1c93172005-04-21 23:48:37 +00002183
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002184 return IC->InsertNewInstBefore(CastInst::create(
2185 CI->getOpcode(), SO, I.getType(), SO->getName() + ".cast"), I);
Chris Lattner86102b82005-01-01 16:22:27 +00002186 }
2187
Chris Lattner183b3362004-04-09 19:05:30 +00002188 // Figure out if the constant is the left or the right argument.
Chris Lattner86102b82005-01-01 16:22:27 +00002189 bool ConstIsRHS = isa<Constant>(I.getOperand(1));
2190 Constant *ConstOperand = cast<Constant>(I.getOperand(ConstIsRHS));
Chris Lattnerb8b97502003-08-13 19:01:45 +00002191
Chris Lattner183b3362004-04-09 19:05:30 +00002192 if (Constant *SOC = dyn_cast<Constant>(SO)) {
2193 if (ConstIsRHS)
Chris Lattner86102b82005-01-01 16:22:27 +00002194 return ConstantExpr::get(I.getOpcode(), SOC, ConstOperand);
2195 return ConstantExpr::get(I.getOpcode(), ConstOperand, SOC);
Chris Lattner183b3362004-04-09 19:05:30 +00002196 }
2197
2198 Value *Op0 = SO, *Op1 = ConstOperand;
2199 if (!ConstIsRHS)
2200 std::swap(Op0, Op1);
2201 Instruction *New;
Chris Lattner86102b82005-01-01 16:22:27 +00002202 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(&I))
2203 New = BinaryOperator::create(BO->getOpcode(), Op0, Op1,SO->getName()+".op");
Reid Spencer266e42b2006-12-23 06:05:41 +00002204 else if (CmpInst *CI = dyn_cast<CmpInst>(&I))
2205 New = CmpInst::create(CI->getOpcode(), CI->getPredicate(), Op0, Op1,
2206 SO->getName()+".cmp");
Chris Lattnerf9d96652004-04-10 19:15:56 +00002207 else {
Chris Lattner183b3362004-04-09 19:05:30 +00002208 assert(0 && "Unknown binary instruction type!");
Chris Lattnerf9d96652004-04-10 19:15:56 +00002209 abort();
2210 }
Chris Lattner86102b82005-01-01 16:22:27 +00002211 return IC->InsertNewInstBefore(New, I);
2212}
2213
2214// FoldOpIntoSelect - Given an instruction with a select as one operand and a
2215// constant as the other operand, try to fold the binary operator into the
2216// select arguments. This also works for Cast instructions, which obviously do
2217// not have a second operand.
2218static Instruction *FoldOpIntoSelect(Instruction &Op, SelectInst *SI,
2219 InstCombiner *IC) {
2220 // Don't modify shared select instructions
2221 if (!SI->hasOneUse()) return 0;
2222 Value *TV = SI->getOperand(1);
2223 Value *FV = SI->getOperand(2);
2224
2225 if (isa<Constant>(TV) || isa<Constant>(FV)) {
Chris Lattner374e6592005-04-21 05:43:13 +00002226 // Bool selects with constant operands can be folded to logical ops.
Reid Spencer542964f2007-01-11 18:21:29 +00002227 if (SI->getType() == Type::Int1Ty) return 0;
Chris Lattner374e6592005-04-21 05:43:13 +00002228
Chris Lattner86102b82005-01-01 16:22:27 +00002229 Value *SelectTrueVal = FoldOperationIntoSelectOperand(Op, TV, IC);
2230 Value *SelectFalseVal = FoldOperationIntoSelectOperand(Op, FV, IC);
2231
Gabor Greife9ecc682008-04-06 20:25:17 +00002232 return SelectInst::Create(SI->getCondition(), SelectTrueVal,
2233 SelectFalseVal);
Chris Lattner86102b82005-01-01 16:22:27 +00002234 }
2235 return 0;
Chris Lattner183b3362004-04-09 19:05:30 +00002236}
2237
Chris Lattner6a4adcd2004-09-29 05:07:12 +00002238
2239/// FoldOpIntoPhi - Given a binary operator or cast instruction which has a PHI
2240/// node as operand #0, see if we can fold the instruction into the PHI (which
2241/// is only possible if all operands to the PHI are constants).
2242Instruction *InstCombiner::FoldOpIntoPhi(Instruction &I) {
2243 PHINode *PN = cast<PHINode>(I.getOperand(0));
Chris Lattner7515cab2004-11-14 19:13:23 +00002244 unsigned NumPHIValues = PN->getNumIncomingValues();
Chris Lattner04689872006-09-09 22:02:56 +00002245 if (!PN->hasOneUse() || NumPHIValues == 0) return 0;
Chris Lattner6a4adcd2004-09-29 05:07:12 +00002246
Chris Lattner04689872006-09-09 22:02:56 +00002247 // Check to see if all of the operands of the PHI are constants. If there is
2248 // one non-constant value, remember the BB it is. If there is more than one
Chris Lattnerc4d8e7e2007-02-24 01:03:45 +00002249 // or if *it* is a PHI, bail out.
Chris Lattner04689872006-09-09 22:02:56 +00002250 BasicBlock *NonConstBB = 0;
2251 for (unsigned i = 0; i != NumPHIValues; ++i)
2252 if (!isa<Constant>(PN->getIncomingValue(i))) {
2253 if (NonConstBB) return 0; // More than one non-const value.
Chris Lattnerc4d8e7e2007-02-24 01:03:45 +00002254 if (isa<PHINode>(PN->getIncomingValue(i))) return 0; // Itself a phi.
Chris Lattner04689872006-09-09 22:02:56 +00002255 NonConstBB = PN->getIncomingBlock(i);
2256
2257 // If the incoming non-constant value is in I's block, we have an infinite
2258 // loop.
2259 if (NonConstBB == I.getParent())
2260 return 0;
2261 }
2262
2263 // If there is exactly one non-constant value, we can insert a copy of the
2264 // operation in that block. However, if this is a critical edge, we would be
2265 // inserting the computation one some other paths (e.g. inside a loop). Only
2266 // do this if the pred block is unconditionally branching into the phi block.
2267 if (NonConstBB) {
2268 BranchInst *BI = dyn_cast<BranchInst>(NonConstBB->getTerminator());
2269 if (!BI || !BI->isUnconditional()) return 0;
2270 }
Chris Lattner6a4adcd2004-09-29 05:07:12 +00002271
2272 // Okay, we can do the transformation: create the new PHI node.
Gabor Greife9ecc682008-04-06 20:25:17 +00002273 PHINode *NewPN = PHINode::Create(I.getType(), "");
Chris Lattnerd8e20182005-01-29 00:39:08 +00002274 NewPN->reserveOperandSpace(PN->getNumOperands()/2);
Chris Lattner6a4adcd2004-09-29 05:07:12 +00002275 InsertNewInstBefore(NewPN, *PN);
Chris Lattner6e0123b2007-02-11 01:23:03 +00002276 NewPN->takeName(PN);
Chris Lattner6a4adcd2004-09-29 05:07:12 +00002277
2278 // Next, add all of the operands to the PHI.
2279 if (I.getNumOperands() == 2) {
2280 Constant *C = cast<Constant>(I.getOperand(1));
Chris Lattner7515cab2004-11-14 19:13:23 +00002281 for (unsigned i = 0; i != NumPHIValues; ++i) {
Chris Lattnerf0da7972007-08-05 08:47:58 +00002282 Value *InV = 0;
Chris Lattner04689872006-09-09 22:02:56 +00002283 if (Constant *InC = dyn_cast<Constant>(PN->getIncomingValue(i))) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002284 if (CmpInst *CI = dyn_cast<CmpInst>(&I))
2285 InV = ConstantExpr::getCompare(CI->getPredicate(), InC, C);
2286 else
2287 InV = ConstantExpr::get(I.getOpcode(), InC, C);
Chris Lattner04689872006-09-09 22:02:56 +00002288 } else {
2289 assert(PN->getIncomingBlock(i) == NonConstBB);
2290 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(&I))
2291 InV = BinaryOperator::create(BO->getOpcode(),
2292 PN->getIncomingValue(i), C, "phitmp",
2293 NonConstBB->getTerminator());
Reid Spencer266e42b2006-12-23 06:05:41 +00002294 else if (CmpInst *CI = dyn_cast<CmpInst>(&I))
2295 InV = CmpInst::create(CI->getOpcode(),
2296 CI->getPredicate(),
2297 PN->getIncomingValue(i), C, "phitmp",
2298 NonConstBB->getTerminator());
Chris Lattner04689872006-09-09 22:02:56 +00002299 else
2300 assert(0 && "Unknown binop!");
2301
Chris Lattnerb15e2b12007-03-02 21:28:56 +00002302 AddToWorkList(cast<Instruction>(InV));
Chris Lattner04689872006-09-09 22:02:56 +00002303 }
2304 NewPN->addIncoming(InV, PN->getIncomingBlock(i));
Chris Lattner6a4adcd2004-09-29 05:07:12 +00002305 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002306 } else {
2307 CastInst *CI = cast<CastInst>(&I);
2308 const Type *RetTy = CI->getType();
Chris Lattner7515cab2004-11-14 19:13:23 +00002309 for (unsigned i = 0; i != NumPHIValues; ++i) {
Chris Lattner04689872006-09-09 22:02:56 +00002310 Value *InV;
2311 if (Constant *InC = dyn_cast<Constant>(PN->getIncomingValue(i))) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002312 InV = ConstantExpr::getCast(CI->getOpcode(), InC, RetTy);
Chris Lattner04689872006-09-09 22:02:56 +00002313 } else {
2314 assert(PN->getIncomingBlock(i) == NonConstBB);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002315 InV = CastInst::create(CI->getOpcode(), PN->getIncomingValue(i),
2316 I.getType(), "phitmp",
2317 NonConstBB->getTerminator());
Chris Lattnerb15e2b12007-03-02 21:28:56 +00002318 AddToWorkList(cast<Instruction>(InV));
Chris Lattner04689872006-09-09 22:02:56 +00002319 }
2320 NewPN->addIncoming(InV, PN->getIncomingBlock(i));
Chris Lattner6a4adcd2004-09-29 05:07:12 +00002321 }
2322 }
2323 return ReplaceInstUsesWith(I, NewPN);
2324}
2325
Chris Lattner17819d92008-01-29 06:52:45 +00002326
2327/// CannotBeNegativeZero - Return true if we can prove that the specified FP
2328/// value is never equal to -0.0.
2329///
2330/// Note that this function will need to be revisited when we support nondefault
2331/// rounding modes!
2332///
2333static bool CannotBeNegativeZero(const Value *V) {
2334 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(V))
2335 return !CFP->getValueAPF().isNegZero();
2336
2337 // (add x, 0.0) is guaranteed to return +0.0, not -0.0.
2338 if (const Instruction *I = dyn_cast<Instruction>(V)) {
2339 if (I->getOpcode() == Instruction::Add &&
2340 isa<ConstantFP>(I->getOperand(1)) &&
2341 cast<ConstantFP>(I->getOperand(1))->isNullValue())
2342 return true;
2343
2344 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I))
2345 if (II->getIntrinsicID() == Intrinsic::sqrt)
2346 return CannotBeNegativeZero(II->getOperand(1));
2347
2348 if (const CallInst *CI = dyn_cast<CallInst>(I))
2349 if (const Function *F = CI->getCalledFunction()) {
2350 if (F->isDeclaration()) {
2351 switch (F->getNameLen()) {
2352 case 3: // abs(x) != -0.0
2353 if (!strcmp(F->getNameStart(), "abs")) return true;
2354 break;
2355 case 4: // abs[lf](x) != -0.0
2356 if (!strcmp(F->getNameStart(), "absf")) return true;
2357 if (!strcmp(F->getNameStart(), "absl")) return true;
2358 break;
2359 }
2360 }
2361 }
2362 }
2363
2364 return false;
2365}
2366
2367
Chris Lattner113f4f42002-06-25 16:13:24 +00002368Instruction *InstCombiner::visitAdd(BinaryOperator &I) {
Chris Lattnerdcf240a2003-03-10 21:43:22 +00002369 bool Changed = SimplifyCommutative(I);
Chris Lattner113f4f42002-06-25 16:13:24 +00002370 Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
Chris Lattner9fa53de2002-05-06 16:49:18 +00002371
Chris Lattnercf4a9962004-04-10 22:01:55 +00002372 if (Constant *RHSC = dyn_cast<Constant>(RHS)) {
Chris Lattner81a7a232004-10-16 18:11:37 +00002373 // X + undef -> undef
2374 if (isa<UndefValue>(RHS))
2375 return ReplaceInstUsesWith(I, RHS);
2376
Chris Lattnercf4a9962004-04-10 22:01:55 +00002377 // X + 0 --> X
Chris Lattner7a002fe2006-12-02 00:13:08 +00002378 if (!I.getType()->isFPOrFPVector()) { // NOTE: -0 + +0 = +0.
Chris Lattner7fde91e2005-10-17 17:56:38 +00002379 if (RHSC->isNullValue())
2380 return ReplaceInstUsesWith(I, LHS);
Chris Lattnerda1b1522005-10-17 20:18:38 +00002381 } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHSC)) {
Dale Johannesen98d3a082007-09-14 22:26:36 +00002382 if (CFP->isExactlyValue(ConstantFP::getNegativeZero
2383 (I.getType())->getValueAPF()))
Chris Lattnerda1b1522005-10-17 20:18:38 +00002384 return ReplaceInstUsesWith(I, LHS);
Chris Lattner7fde91e2005-10-17 17:56:38 +00002385 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00002386
Chris Lattnercf4a9962004-04-10 22:01:55 +00002387 if (ConstantInt *CI = dyn_cast<ConstantInt>(RHSC)) {
Chris Lattner6e2c15c2006-11-09 05:12:27 +00002388 // X + (signbit) --> X ^ signbit
Zhou Sheng150f3bb2007-04-01 17:13:37 +00002389 const APInt& Val = CI->getValue();
Zhou Sheng56cda952007-04-02 08:20:41 +00002390 uint32_t BitWidth = Val.getBitWidth();
Reid Spencer959a21d2007-03-23 21:24:59 +00002391 if (Val == APInt::getSignBit(BitWidth))
Chris Lattnerdf20a4d2004-06-10 02:07:29 +00002392 return BinaryOperator::createXor(LHS, RHS);
Chris Lattner6e2c15c2006-11-09 05:12:27 +00002393
2394 // See if SimplifyDemandedBits can simplify this. This handles stuff like
2395 // (X & 254)+1 -> (X&254)|1
Reid Spencer959a21d2007-03-23 21:24:59 +00002396 if (!isa<VectorType>(I.getType())) {
2397 APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0);
2398 if (SimplifyDemandedBits(&I, APInt::getAllOnesValue(BitWidth),
2399 KnownZero, KnownOne))
2400 return &I;
2401 }
Chris Lattnercf4a9962004-04-10 22:01:55 +00002402 }
Chris Lattner6a4adcd2004-09-29 05:07:12 +00002403
2404 if (isa<PHINode>(LHS))
2405 if (Instruction *NV = FoldOpIntoPhi(I))
2406 return NV;
Chris Lattner0b3557f2005-09-24 23:43:33 +00002407
Chris Lattner330628a2006-01-06 17:59:59 +00002408 ConstantInt *XorRHS = 0;
2409 Value *XorLHS = 0;
Chris Lattner4284f642007-01-30 22:32:46 +00002410 if (isa<ConstantInt>(RHSC) &&
2411 match(LHS, m_Xor(m_Value(XorLHS), m_ConstantInt(XorRHS)))) {
Zhou Sheng56cda952007-04-02 08:20:41 +00002412 uint32_t TySizeBits = I.getType()->getPrimitiveSizeInBits();
Zhou Sheng150f3bb2007-04-01 17:13:37 +00002413 const APInt& RHSVal = cast<ConstantInt>(RHSC)->getValue();
Chris Lattner0b3557f2005-09-24 23:43:33 +00002414
Zhou Sheng56cda952007-04-02 08:20:41 +00002415 uint32_t Size = TySizeBits / 2;
Reid Spencer959a21d2007-03-23 21:24:59 +00002416 APInt C0080Val(APInt(TySizeBits, 1ULL).shl(Size - 1));
2417 APInt CFF80Val(-C0080Val);
Chris Lattner0b3557f2005-09-24 23:43:33 +00002418 do {
2419 if (TySizeBits > Size) {
Chris Lattner0b3557f2005-09-24 23:43:33 +00002420 // If we have ADD(XOR(AND(X, 0xFF), 0x80), 0xF..F80), it's a sext.
2421 // If we have ADD(XOR(AND(X, 0xFF), 0xF..F80), 0x80), it's a sext.
Reid Spencer959a21d2007-03-23 21:24:59 +00002422 if ((RHSVal == CFF80Val && XorRHS->getValue() == C0080Val) ||
2423 (RHSVal == C0080Val && XorRHS->getValue() == CFF80Val)) {
Chris Lattner0b3557f2005-09-24 23:43:33 +00002424 // This is a sign extend if the top bits are known zero.
Zhou Shengb3a80b12007-03-29 08:15:12 +00002425 if (!MaskedValueIsZero(XorLHS,
2426 APInt::getHighBitsSet(TySizeBits, TySizeBits - Size)))
Chris Lattner0b3557f2005-09-24 23:43:33 +00002427 Size = 0; // Not a sign ext, but can't be any others either.
Reid Spencer959a21d2007-03-23 21:24:59 +00002428 break;
Chris Lattner0b3557f2005-09-24 23:43:33 +00002429 }
2430 }
2431 Size >>= 1;
Reid Spencer959a21d2007-03-23 21:24:59 +00002432 C0080Val = APIntOps::lshr(C0080Val, Size);
2433 CFF80Val = APIntOps::ashr(CFF80Val, Size);
2434 } while (Size >= 1);
Chris Lattner0b3557f2005-09-24 23:43:33 +00002435
Reid Spencera5c18bf2007-03-28 01:36:16 +00002436 // FIXME: This shouldn't be necessary. When the backends can handle types
2437 // with funny bit widths then this whole cascade of if statements should
2438 // be removed. It is just here to get the size of the "middle" type back
2439 // up to something that the back ends can handle.
2440 const Type *MiddleType = 0;
2441 switch (Size) {
2442 default: break;
2443 case 32: MiddleType = Type::Int32Ty; break;
2444 case 16: MiddleType = Type::Int16Ty; break;
2445 case 8: MiddleType = Type::Int8Ty; break;
2446 }
2447 if (MiddleType) {
Reid Spencerbb65ebf2006-12-12 23:36:14 +00002448 Instruction *NewTrunc = new TruncInst(XorLHS, MiddleType, "sext");
Chris Lattner0b3557f2005-09-24 23:43:33 +00002449 InsertNewInstBefore(NewTrunc, I);
Reid Spencera5c18bf2007-03-28 01:36:16 +00002450 return new SExtInst(NewTrunc, I.getType(), I.getName());
Chris Lattner0b3557f2005-09-24 23:43:33 +00002451 }
2452 }
Chris Lattnercf4a9962004-04-10 22:01:55 +00002453 }
Chris Lattner9fa53de2002-05-06 16:49:18 +00002454
Chris Lattnerb8b97502003-08-13 19:01:45 +00002455 // X + X --> X << 1
Chris Lattner03c49532007-01-15 02:27:26 +00002456 if (I.getType()->isInteger() && I.getType() != Type::Int1Ty) {
Chris Lattnerb8b97502003-08-13 19:01:45 +00002457 if (Instruction *Result = AssociativeOpt(I, AddRHS(RHS))) return Result;
Chris Lattner47060462005-04-07 17:14:51 +00002458
2459 if (Instruction *RHSI = dyn_cast<Instruction>(RHS)) {
2460 if (RHSI->getOpcode() == Instruction::Sub)
2461 if (LHS == RHSI->getOperand(1)) // A + (B - A) --> B
2462 return ReplaceInstUsesWith(I, RHSI->getOperand(0));
2463 }
2464 if (Instruction *LHSI = dyn_cast<Instruction>(LHS)) {
2465 if (LHSI->getOpcode() == Instruction::Sub)
2466 if (RHS == LHSI->getOperand(1)) // (B - A) + A --> B
2467 return ReplaceInstUsesWith(I, LHSI->getOperand(0));
2468 }
Robert Bocchino7b5b86c2004-07-27 21:02:21 +00002469 }
Chris Lattnerede3fe02003-08-13 04:18:28 +00002470
Chris Lattner147e9752002-05-08 22:46:53 +00002471 // -A + B --> B - A
Chris Lattnercc226012008-02-17 21:03:36 +00002472 // -A + -B --> -(A + B)
2473 if (Value *LHSV = dyn_castNegVal(LHS)) {
Chris Lattner1e3c5012008-02-18 17:50:16 +00002474 if (LHS->getType()->isIntOrIntVector()) {
2475 if (Value *RHSV = dyn_castNegVal(RHS)) {
2476 Instruction *NewAdd = BinaryOperator::createAdd(LHSV, RHSV, "sum");
2477 InsertNewInstBefore(NewAdd, I);
2478 return BinaryOperator::createNeg(NewAdd);
2479 }
Chris Lattnercc226012008-02-17 21:03:36 +00002480 }
2481
2482 return BinaryOperator::createSub(RHS, LHSV);
2483 }
Chris Lattner9fa53de2002-05-06 16:49:18 +00002484
2485 // A + -B --> A - B
Chris Lattnerbb74e222003-03-10 23:06:50 +00002486 if (!isa<Constant>(RHS))
2487 if (Value *V = dyn_castNegVal(RHS))
Chris Lattnerdf20a4d2004-06-10 02:07:29 +00002488 return BinaryOperator::createSub(LHS, V);
Chris Lattner260ab202002-04-18 17:39:14 +00002489
Misha Brukmanb1c93172005-04-21 23:48:37 +00002490
Chris Lattner8c3e7b92004-11-13 19:50:12 +00002491 ConstantInt *C2;
2492 if (Value *X = dyn_castFoldableMul(LHS, C2)) {
2493 if (X == RHS) // X*C + X --> X * (C+1)
2494 return BinaryOperator::createMul(RHS, AddOne(C2));
2495
2496 // X*C1 + X*C2 --> X * (C1+C2)
2497 ConstantInt *C1;
2498 if (X == dyn_castFoldableMul(RHS, C1))
Reid Spencer80263aa2007-03-25 05:33:51 +00002499 return BinaryOperator::createMul(X, Add(C1, C2));
Chris Lattner57c8d992003-02-18 19:57:07 +00002500 }
2501
2502 // X + X*C --> X * (C+1)
Chris Lattner8c3e7b92004-11-13 19:50:12 +00002503 if (dyn_castFoldableMul(RHS, C2) == LHS)
2504 return BinaryOperator::createMul(LHS, AddOne(C2));
2505
Chris Lattner23eb8ec2007-01-05 02:17:46 +00002506 // X + ~X --> -1 since ~X = -X-1
Chris Lattner37338922007-06-15 06:23:19 +00002507 if (dyn_castNotVal(LHS) == RHS || dyn_castNotVal(RHS) == LHS)
2508 return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType()));
Chris Lattner23eb8ec2007-01-05 02:17:46 +00002509
Chris Lattner57c8d992003-02-18 19:57:07 +00002510
Chris Lattnerb8b97502003-08-13 19:01:45 +00002511 // (A & C1)+(B & C2) --> (A & C1)|(B & C2) iff C1&C2 == 0
Chris Lattnerd4252a72004-07-30 07:50:03 +00002512 if (match(RHS, m_And(m_Value(), m_ConstantInt(C2))))
Chris Lattner23eb8ec2007-01-05 02:17:46 +00002513 if (Instruction *R = AssociativeOpt(I, AddMaskingAnd(C2)))
2514 return R;
Chris Lattner7fb29e12003-03-11 00:12:48 +00002515
Nick Lewyckye6e3a7f2008-02-03 07:42:09 +00002516 // W*X + Y*Z --> W * (X+Z) iff W == Y
Nick Lewyckyc7a4ba02008-02-03 08:19:11 +00002517 if (I.getType()->isIntOrIntVector()) {
Nick Lewyckye6e3a7f2008-02-03 07:42:09 +00002518 Value *W, *X, *Y, *Z;
2519 if (match(LHS, m_Mul(m_Value(W), m_Value(X))) &&
2520 match(RHS, m_Mul(m_Value(Y), m_Value(Z)))) {
2521 if (W != Y) {
2522 if (W == Z) {
Bill Wendlingd188e032008-02-26 10:53:30 +00002523 std::swap(Y, Z);
Nick Lewyckye6e3a7f2008-02-03 07:42:09 +00002524 } else if (Y == X) {
Bill Wendlingd188e032008-02-26 10:53:30 +00002525 std::swap(W, X);
2526 } else if (X == Z) {
Nick Lewyckye6e3a7f2008-02-03 07:42:09 +00002527 std::swap(Y, Z);
2528 std::swap(W, X);
2529 }
2530 }
2531
2532 if (W == Y) {
2533 Value *NewAdd = InsertNewInstBefore(BinaryOperator::createAdd(X, Z,
2534 LHS->getName()), I);
2535 return BinaryOperator::createMul(W, NewAdd);
2536 }
2537 }
2538 }
2539
Chris Lattnerb9cde762003-10-02 15:11:26 +00002540 if (ConstantInt *CRHS = dyn_cast<ConstantInt>(RHS)) {
Chris Lattner330628a2006-01-06 17:59:59 +00002541 Value *X = 0;
Reid Spencer80263aa2007-03-25 05:33:51 +00002542 if (match(LHS, m_Not(m_Value(X)))) // ~X + C --> (C-1) - X
2543 return BinaryOperator::createSub(SubOne(CRHS), X);
Chris Lattnerd4252a72004-07-30 07:50:03 +00002544
Chris Lattnerbff91d92004-10-08 05:07:56 +00002545 // (X & FF00) + xx00 -> (X+xx00) & FF00
2546 if (LHS->hasOneUse() && match(LHS, m_And(m_Value(X), m_ConstantInt(C2)))) {
Reid Spencer80263aa2007-03-25 05:33:51 +00002547 Constant *Anded = And(CRHS, C2);
Chris Lattnerbff91d92004-10-08 05:07:56 +00002548 if (Anded == CRHS) {
2549 // See if all bits from the first bit set in the Add RHS up are included
2550 // in the mask. First, get the rightmost bit.
Zhou Sheng150f3bb2007-04-01 17:13:37 +00002551 const APInt& AddRHSV = CRHS->getValue();
Chris Lattnerbff91d92004-10-08 05:07:56 +00002552
2553 // Form a mask of all bits from the lowest bit added through the top.
Zhou Sheng150f3bb2007-04-01 17:13:37 +00002554 APInt AddRHSHighBits(~((AddRHSV & -AddRHSV)-1));
Chris Lattnerbff91d92004-10-08 05:07:56 +00002555
2556 // See if the and mask includes all of these bits.
Zhou Sheng150f3bb2007-04-01 17:13:37 +00002557 APInt AddRHSHighBitsAnd(AddRHSHighBits & C2->getValue());
Misha Brukmanb1c93172005-04-21 23:48:37 +00002558
Chris Lattnerbff91d92004-10-08 05:07:56 +00002559 if (AddRHSHighBits == AddRHSHighBitsAnd) {
2560 // Okay, the xform is safe. Insert the new add pronto.
2561 Value *NewAdd = InsertNewInstBefore(BinaryOperator::createAdd(X, CRHS,
2562 LHS->getName()), I);
2563 return BinaryOperator::createAnd(NewAdd, C2);
2564 }
2565 }
2566 }
2567
Chris Lattnerd4252a72004-07-30 07:50:03 +00002568 // Try to fold constant add into select arguments.
2569 if (SelectInst *SI = dyn_cast<SelectInst>(LHS))
Chris Lattner86102b82005-01-01 16:22:27 +00002570 if (Instruction *R = FoldOpIntoSelect(I, SI, this))
Chris Lattnerd4252a72004-07-30 07:50:03 +00002571 return R;
Chris Lattnerb9cde762003-10-02 15:11:26 +00002572 }
2573
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002574 // add (cast *A to intptrtype) B ->
Chris Lattner16a51da2007-12-20 01:56:58 +00002575 // cast (GEP (cast *A to sbyte*) B) --> intptrtype
Andrew Lenharth4f339be2006-09-19 18:24:51 +00002576 {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002577 CastInst *CI = dyn_cast<CastInst>(LHS);
2578 Value *Other = RHS;
Andrew Lenharth4f339be2006-09-19 18:24:51 +00002579 if (!CI) {
2580 CI = dyn_cast<CastInst>(RHS);
2581 Other = LHS;
2582 }
Andrew Lenharth44cb67a2006-09-20 15:37:57 +00002583 if (CI && CI->getType()->isSized() &&
Reid Spencer8f166b02007-01-08 16:32:00 +00002584 (CI->getType()->getPrimitiveSizeInBits() ==
2585 TD->getIntPtrType()->getPrimitiveSizeInBits())
Andrew Lenharth44cb67a2006-09-20 15:37:57 +00002586 && isa<PointerType>(CI->getOperand(0)->getType())) {
Christopher Lambedf07882007-12-17 01:12:55 +00002587 unsigned AS =
2588 cast<PointerType>(CI->getOperand(0)->getType())->getAddressSpace();
Chris Lattner5a866122008-01-13 22:23:22 +00002589 Value *I2 = InsertBitCastBefore(CI->getOperand(0),
2590 PointerType::get(Type::Int8Ty, AS), I);
Gabor Greife9ecc682008-04-06 20:25:17 +00002591 I2 = InsertNewInstBefore(GetElementPtrInst::Create(I2, Other, "ctg2"), I);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002592 return new PtrToIntInst(I2, CI->getType());
Andrew Lenharth4f339be2006-09-19 18:24:51 +00002593 }
2594 }
Christopher Lamb8b09a462007-12-18 09:34:41 +00002595
Chris Lattner16a51da2007-12-20 01:56:58 +00002596 // add (select X 0 (sub n A)) A --> select X A n
Christopher Lamb8b09a462007-12-18 09:34:41 +00002597 {
2598 SelectInst *SI = dyn_cast<SelectInst>(LHS);
2599 Value *Other = RHS;
2600 if (!SI) {
2601 SI = dyn_cast<SelectInst>(RHS);
2602 Other = LHS;
2603 }
Chris Lattner16a51da2007-12-20 01:56:58 +00002604 if (SI && SI->hasOneUse()) {
Christopher Lamb8b09a462007-12-18 09:34:41 +00002605 Value *TV = SI->getTrueValue();
2606 Value *FV = SI->getFalseValue();
Chris Lattner16a51da2007-12-20 01:56:58 +00002607 Value *A, *N;
Christopher Lamb8b09a462007-12-18 09:34:41 +00002608
2609 // Can we fold the add into the argument of the select?
2610 // We check both true and false select arguments for a matching subtract.
Chris Lattner16a51da2007-12-20 01:56:58 +00002611 if (match(FV, m_Zero()) && match(TV, m_Sub(m_Value(N), m_Value(A))) &&
2612 A == Other) // Fold the add into the true select value.
Gabor Greife9ecc682008-04-06 20:25:17 +00002613 return SelectInst::Create(SI->getCondition(), N, A);
Chris Lattner16a51da2007-12-20 01:56:58 +00002614 if (match(TV, m_Zero()) && match(FV, m_Sub(m_Value(N), m_Value(A))) &&
2615 A == Other) // Fold the add into the false select value.
Gabor Greife9ecc682008-04-06 20:25:17 +00002616 return SelectInst::Create(SI->getCondition(), A, N);
Christopher Lamb8b09a462007-12-18 09:34:41 +00002617 }
2618 }
Chris Lattner17819d92008-01-29 06:52:45 +00002619
2620 // Check for X+0.0. Simplify it to X if we know X is not -0.0.
2621 if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHS))
2622 if (CFP->getValueAPF().isPosZero() && CannotBeNegativeZero(LHS))
2623 return ReplaceInstUsesWith(I, LHS);
Andrew Lenharth4f339be2006-09-19 18:24:51 +00002624
Chris Lattner113f4f42002-06-25 16:13:24 +00002625 return Changed ? &I : 0;
Chris Lattner260ab202002-04-18 17:39:14 +00002626}
2627
Chris Lattnerbdb0ce02003-07-22 21:46:59 +00002628// isSignBit - Return true if the value represented by the constant only has the
2629// highest order bit set.
2630static bool isSignBit(ConstantInt *CI) {
Zhou Sheng56cda952007-04-02 08:20:41 +00002631 uint32_t NumBits = CI->getType()->getPrimitiveSizeInBits();
Reid Spencer450434e2007-03-19 20:58:18 +00002632 return CI->getValue() == APInt::getSignBit(NumBits);
Chris Lattnerbdb0ce02003-07-22 21:46:59 +00002633}
2634
Chris Lattner113f4f42002-06-25 16:13:24 +00002635Instruction *InstCombiner::visitSub(BinaryOperator &I) {
Chris Lattner113f4f42002-06-25 16:13:24 +00002636 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
Chris Lattnerf4cdbf32002-05-06 16:14:14 +00002637
Chris Lattnere6794492002-08-12 21:17:25 +00002638 if (Op0 == Op1) // sub X, X -> 0
2639 return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
Chris Lattner260ab202002-04-18 17:39:14 +00002640
Chris Lattnere6794492002-08-12 21:17:25 +00002641 // If this is a 'B = x-(-A)', change to B = x+A...
Chris Lattnerbb74e222003-03-10 23:06:50 +00002642 if (Value *V = dyn_castNegVal(Op1))
Chris Lattnerdf20a4d2004-06-10 02:07:29 +00002643 return BinaryOperator::createAdd(Op0, V);
Chris Lattner9fa53de2002-05-06 16:49:18 +00002644
Chris Lattner81a7a232004-10-16 18:11:37 +00002645 if (isa<UndefValue>(Op0))
2646 return ReplaceInstUsesWith(I, Op0); // undef - X -> undef
2647 if (isa<UndefValue>(Op1))
2648 return ReplaceInstUsesWith(I, Op1); // X - undef -> undef
2649
Chris Lattner8f2f5982003-11-05 01:06:05 +00002650 if (ConstantInt *C = dyn_cast<ConstantInt>(Op0)) {
2651 // Replace (-1 - A) with (~A)...
Chris Lattner3082c5a2003-02-18 19:28:33 +00002652 if (C->isAllOnesValue())
2653 return BinaryOperator::createNot(Op1);
Chris Lattnerad3c4952002-05-09 01:29:19 +00002654
Chris Lattner8f2f5982003-11-05 01:06:05 +00002655 // C - ~X == X + (1+C)
Reid Spencer4fdd96c2005-06-18 17:37:34 +00002656 Value *X = 0;
Chris Lattnerd4252a72004-07-30 07:50:03 +00002657 if (match(Op1, m_Not(m_Value(X))))
Reid Spencer80263aa2007-03-25 05:33:51 +00002658 return BinaryOperator::createAdd(X, AddOne(C));
2659
Chris Lattner27df1db2007-01-15 07:02:54 +00002660 // -(X >>u 31) -> (X >>s 31)
2661 // -(X >>s 31) -> (X >>u 31)
Zhou Shengfd28a332007-03-30 17:20:39 +00002662 if (C->isZero()) {
Anton Korobeynikov1bfd1212008-02-20 11:26:25 +00002663 if (BinaryOperator *SI = dyn_cast<BinaryOperator>(Op1)) {
Reid Spencerfdff9382006-11-08 06:47:33 +00002664 if (SI->getOpcode() == Instruction::LShr) {
Reid Spencere0fc4df2006-10-20 07:07:24 +00002665 if (ConstantInt *CU = dyn_cast<ConstantInt>(SI->getOperand(1))) {
Chris Lattner92295c52004-03-12 23:53:13 +00002666 // Check to see if we are shifting out everything but the sign bit.
Zhou Shengfd28a332007-03-30 17:20:39 +00002667 if (CU->getLimitedValue(SI->getType()->getPrimitiveSizeInBits()) ==
Reid Spencere0fc4df2006-10-20 07:07:24 +00002668 SI->getType()->getPrimitiveSizeInBits()-1) {
Reid Spencerfdff9382006-11-08 06:47:33 +00002669 // Ok, the transformation is safe. Insert AShr.
Reid Spencer2341c222007-02-02 02:16:23 +00002670 return BinaryOperator::create(Instruction::AShr,
2671 SI->getOperand(0), CU, SI->getName());
Chris Lattner92295c52004-03-12 23:53:13 +00002672 }
2673 }
Reid Spencerfdff9382006-11-08 06:47:33 +00002674 }
2675 else if (SI->getOpcode() == Instruction::AShr) {
2676 if (ConstantInt *CU = dyn_cast<ConstantInt>(SI->getOperand(1))) {
2677 // Check to see if we are shifting out everything but the sign bit.
Zhou Shengfd28a332007-03-30 17:20:39 +00002678 if (CU->getLimitedValue(SI->getType()->getPrimitiveSizeInBits()) ==
Reid Spencerfdff9382006-11-08 06:47:33 +00002679 SI->getType()->getPrimitiveSizeInBits()-1) {
Reid Spencerc635f472006-12-31 05:48:39 +00002680 // Ok, the transformation is safe. Insert LShr.
Reid Spencer0d5f9232007-02-02 14:08:20 +00002681 return BinaryOperator::createLShr(
Reid Spencer2341c222007-02-02 02:16:23 +00002682 SI->getOperand(0), CU, SI->getName());
Reid Spencerfdff9382006-11-08 06:47:33 +00002683 }
2684 }
Anton Korobeynikov1bfd1212008-02-20 11:26:25 +00002685 }
2686 }
Chris Lattner022167f2004-03-13 00:11:49 +00002687 }
Chris Lattner183b3362004-04-09 19:05:30 +00002688
2689 // Try to fold constant sub into select arguments.
2690 if (SelectInst *SI = dyn_cast<SelectInst>(Op1))
Chris Lattner86102b82005-01-01 16:22:27 +00002691 if (Instruction *R = FoldOpIntoSelect(I, SI, this))
Chris Lattner183b3362004-04-09 19:05:30 +00002692 return R;
Chris Lattner6a4adcd2004-09-29 05:07:12 +00002693
2694 if (isa<PHINode>(Op0))
2695 if (Instruction *NV = FoldOpIntoPhi(I))
2696 return NV;
Chris Lattner8f2f5982003-11-05 01:06:05 +00002697 }
2698
Chris Lattnera9be4492005-04-07 16:15:25 +00002699 if (BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1)) {
2700 if (Op1I->getOpcode() == Instruction::Add &&
Chris Lattner7a002fe2006-12-02 00:13:08 +00002701 !Op0->getType()->isFPOrFPVector()) {
Chris Lattnerc7f3c1a2005-04-07 16:28:01 +00002702 if (Op1I->getOperand(0) == Op0) // X-(X+Y) == -Y
Chris Lattnera9be4492005-04-07 16:15:25 +00002703 return BinaryOperator::createNeg(Op1I->getOperand(1), I.getName());
Chris Lattnerc7f3c1a2005-04-07 16:28:01 +00002704 else if (Op1I->getOperand(1) == Op0) // X-(Y+X) == -Y
Chris Lattnera9be4492005-04-07 16:15:25 +00002705 return BinaryOperator::createNeg(Op1I->getOperand(0), I.getName());
Chris Lattnerc7f3c1a2005-04-07 16:28:01 +00002706 else if (ConstantInt *CI1 = dyn_cast<ConstantInt>(I.getOperand(0))) {
2707 if (ConstantInt *CI2 = dyn_cast<ConstantInt>(Op1I->getOperand(1)))
2708 // C1-(X+C2) --> (C1-C2)-X
Reid Spencer80263aa2007-03-25 05:33:51 +00002709 return BinaryOperator::createSub(Subtract(CI1, CI2),
Chris Lattnerc7f3c1a2005-04-07 16:28:01 +00002710 Op1I->getOperand(0));
2711 }
Chris Lattnera9be4492005-04-07 16:15:25 +00002712 }
2713
Chris Lattnerf95d9b92003-10-15 16:48:29 +00002714 if (Op1I->hasOneUse()) {
Chris Lattner3082c5a2003-02-18 19:28:33 +00002715 // Replace (x - (y - z)) with (x + (z - y)) if the (y - z) subexpression
2716 // is not used by anyone else...
2717 //
Chris Lattnerc2f0aa52004-02-02 20:09:56 +00002718 if (Op1I->getOpcode() == Instruction::Sub &&
Chris Lattner7a002fe2006-12-02 00:13:08 +00002719 !Op1I->getType()->isFPOrFPVector()) {
Chris Lattner3082c5a2003-02-18 19:28:33 +00002720 // Swap the two operands of the subexpr...
2721 Value *IIOp0 = Op1I->getOperand(0), *IIOp1 = Op1I->getOperand(1);
2722 Op1I->setOperand(0, IIOp1);
2723 Op1I->setOperand(1, IIOp0);
Misha Brukmanb1c93172005-04-21 23:48:37 +00002724
Chris Lattner3082c5a2003-02-18 19:28:33 +00002725 // Create the new top level add instruction...
Chris Lattnerdf20a4d2004-06-10 02:07:29 +00002726 return BinaryOperator::createAdd(Op0, Op1);
Chris Lattner3082c5a2003-02-18 19:28:33 +00002727 }
2728
2729 // Replace (A - (A & B)) with (A & ~B) if this is the only use of (A&B)...
2730 //
2731 if (Op1I->getOpcode() == Instruction::And &&
2732 (Op1I->getOperand(0) == Op0 || Op1I->getOperand(1) == Op0)) {
2733 Value *OtherOp = Op1I->getOperand(Op1I->getOperand(0) == Op0);
2734
Chris Lattner396dbfe2004-06-09 05:08:07 +00002735 Value *NewNot =
2736 InsertNewInstBefore(BinaryOperator::createNot(OtherOp, "B.not"), I);
Chris Lattnerdf20a4d2004-06-10 02:07:29 +00002737 return BinaryOperator::createAnd(Op0, NewNot);
Chris Lattner3082c5a2003-02-18 19:28:33 +00002738 }
Chris Lattner57c8d992003-02-18 19:57:07 +00002739
Reid Spencer3c514952006-10-16 23:08:08 +00002740 // 0 - (X sdiv C) -> (X sdiv -C)
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002741 if (Op1I->getOpcode() == Instruction::SDiv)
Reid Spencere0fc4df2006-10-20 07:07:24 +00002742 if (ConstantInt *CSI = dyn_cast<ConstantInt>(Op0))
Zhou Shengaafe4e22007-04-19 05:39:12 +00002743 if (CSI->isZero())
Chris Lattner0aee4b72004-10-06 15:08:25 +00002744 if (Constant *DivRHS = dyn_cast<Constant>(Op1I->getOperand(1)))
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002745 return BinaryOperator::createSDiv(Op1I->getOperand(0),
Chris Lattner0aee4b72004-10-06 15:08:25 +00002746 ConstantExpr::getNeg(DivRHS));
2747
Chris Lattner57c8d992003-02-18 19:57:07 +00002748 // X - X*C --> X * (1-C)
Reid Spencer4fdd96c2005-06-18 17:37:34 +00002749 ConstantInt *C2 = 0;
Chris Lattner8c3e7b92004-11-13 19:50:12 +00002750 if (dyn_castFoldableMul(Op1I, C2) == Op0) {
Reid Spencer80263aa2007-03-25 05:33:51 +00002751 Constant *CP1 = Subtract(ConstantInt::get(I.getType(), 1), C2);
Chris Lattnerdf20a4d2004-06-10 02:07:29 +00002752 return BinaryOperator::createMul(Op0, CP1);
Chris Lattner57c8d992003-02-18 19:57:07 +00002753 }
Dan Gohman2ac26522007-09-17 17:31:57 +00002754
2755 // X - ((X / Y) * Y) --> X % Y
2756 if (Op1I->getOpcode() == Instruction::Mul)
2757 if (Instruction *I = dyn_cast<Instruction>(Op1I->getOperand(0)))
2758 if (Op0 == I->getOperand(0) &&
2759 Op1I->getOperand(1) == I->getOperand(1)) {
2760 if (I->getOpcode() == Instruction::SDiv)
2761 return BinaryOperator::createSRem(Op0, Op1I->getOperand(1));
2762 if (I->getOpcode() == Instruction::UDiv)
2763 return BinaryOperator::createURem(Op0, Op1I->getOperand(1));
2764 }
Chris Lattnerad3c4952002-05-09 01:29:19 +00002765 }
Chris Lattnera9be4492005-04-07 16:15:25 +00002766 }
Chris Lattner3082c5a2003-02-18 19:28:33 +00002767
Chris Lattner7a002fe2006-12-02 00:13:08 +00002768 if (!Op0->getType()->isFPOrFPVector())
Anton Korobeynikov1bfd1212008-02-20 11:26:25 +00002769 if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) {
Chris Lattner47060462005-04-07 17:14:51 +00002770 if (Op0I->getOpcode() == Instruction::Add) {
Chris Lattner411336f2005-01-19 21:50:18 +00002771 if (Op0I->getOperand(0) == Op1) // (Y+X)-Y == X
2772 return ReplaceInstUsesWith(I, Op0I->getOperand(1));
2773 else if (Op0I->getOperand(1) == Op1) // (X+Y)-Y == X
2774 return ReplaceInstUsesWith(I, Op0I->getOperand(0));
Chris Lattner47060462005-04-07 17:14:51 +00002775 } else if (Op0I->getOpcode() == Instruction::Sub) {
2776 if (Op0I->getOperand(0) == Op1) // (X-Y)-X == -Y
2777 return BinaryOperator::createNeg(Op0I->getOperand(1), I.getName());
Chris Lattner411336f2005-01-19 21:50:18 +00002778 }
Anton Korobeynikov1bfd1212008-02-20 11:26:25 +00002779 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00002780
Chris Lattner8c3e7b92004-11-13 19:50:12 +00002781 ConstantInt *C1;
2782 if (Value *X = dyn_castFoldableMul(Op0, C1)) {
Reid Spencer80263aa2007-03-25 05:33:51 +00002783 if (X == Op1) // X*C - X --> X * (C-1)
2784 return BinaryOperator::createMul(Op1, SubOne(C1));
Chris Lattner57c8d992003-02-18 19:57:07 +00002785
Chris Lattner8c3e7b92004-11-13 19:50:12 +00002786 ConstantInt *C2; // X*C1 - X*C2 -> X * (C1-C2)
2787 if (X == dyn_castFoldableMul(Op1, C2))
Zhou Sheng3b8eb702008-02-22 10:00:35 +00002788 return BinaryOperator::createMul(X, Subtract(C1, C2));
Chris Lattner8c3e7b92004-11-13 19:50:12 +00002789 }
Chris Lattnerf4cdbf32002-05-06 16:14:14 +00002790 return 0;
Chris Lattner260ab202002-04-18 17:39:14 +00002791}
2792
Chris Lattner06205d52007-07-15 20:42:37 +00002793/// isSignBitCheck - Given an exploded icmp instruction, return true if the
2794/// comparison only checks the sign bit. If it only checks the sign bit, set
2795/// TrueIfSigned if the result of the comparison is true when the input value is
2796/// signed.
2797static bool isSignBitCheck(ICmpInst::Predicate pred, ConstantInt *RHS,
2798 bool &TrueIfSigned) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002799 switch (pred) {
Chris Lattner06205d52007-07-15 20:42:37 +00002800 case ICmpInst::ICMP_SLT: // True if LHS s< 0
2801 TrueIfSigned = true;
2802 return RHS->isZero();
Chris Lattner640fd512007-07-16 04:15:34 +00002803 case ICmpInst::ICMP_SLE: // True if LHS s<= RHS and RHS == -1
2804 TrueIfSigned = true;
2805 return RHS->isAllOnesValue();
Chris Lattner06205d52007-07-15 20:42:37 +00002806 case ICmpInst::ICMP_SGT: // True if LHS s> -1
2807 TrueIfSigned = false;
2808 return RHS->isAllOnesValue();
Chris Lattner640fd512007-07-16 04:15:34 +00002809 case ICmpInst::ICMP_UGT:
2810 // True if LHS u> RHS and RHS == high-bit-mask - 1
2811 TrueIfSigned = true;
2812 return RHS->getValue() ==
2813 APInt::getSignedMaxValue(RHS->getType()->getPrimitiveSizeInBits());
2814 case ICmpInst::ICMP_UGE:
2815 // True if LHS u>= RHS and RHS == high-bit-mask (2^7, 2^15, 2^31, etc)
2816 TrueIfSigned = true;
2817 return RHS->getValue() ==
2818 APInt::getSignBit(RHS->getType()->getPrimitiveSizeInBits());
Chris Lattner06205d52007-07-15 20:42:37 +00002819 default:
2820 return false;
Chris Lattnere79e8542004-02-23 06:38:22 +00002821 }
Chris Lattnere79e8542004-02-23 06:38:22 +00002822}
2823
Chris Lattner113f4f42002-06-25 16:13:24 +00002824Instruction *InstCombiner::visitMul(BinaryOperator &I) {
Chris Lattnerdcf240a2003-03-10 21:43:22 +00002825 bool Changed = SimplifyCommutative(I);
Chris Lattner3082c5a2003-02-18 19:28:33 +00002826 Value *Op0 = I.getOperand(0);
Chris Lattner260ab202002-04-18 17:39:14 +00002827
Chris Lattner81a7a232004-10-16 18:11:37 +00002828 if (isa<UndefValue>(I.getOperand(1))) // undef * X -> 0
2829 return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
2830
Chris Lattnere6794492002-08-12 21:17:25 +00002831 // Simplify mul instructions with a constant RHS...
Chris Lattner3082c5a2003-02-18 19:28:33 +00002832 if (Constant *Op1 = dyn_cast<Constant>(I.getOperand(1))) {
2833 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
Chris Lattnerede3fe02003-08-13 04:18:28 +00002834
2835 // ((X << C1)*C2) == (X * (C2 << C1))
Reid Spencer2341c222007-02-02 02:16:23 +00002836 if (BinaryOperator *SI = dyn_cast<BinaryOperator>(Op0))
Chris Lattnerede3fe02003-08-13 04:18:28 +00002837 if (SI->getOpcode() == Instruction::Shl)
2838 if (Constant *ShOp = dyn_cast<Constant>(SI->getOperand(1)))
Chris Lattnerdf20a4d2004-06-10 02:07:29 +00002839 return BinaryOperator::createMul(SI->getOperand(0),
2840 ConstantExpr::getShl(CI, ShOp));
Misha Brukmanb1c93172005-04-21 23:48:37 +00002841
Zhou Shengaafe4e22007-04-19 05:39:12 +00002842 if (CI->isZero())
Chris Lattnercce81be2003-09-11 22:24:54 +00002843 return ReplaceInstUsesWith(I, Op1); // X * 0 == 0
2844 if (CI->equalsInt(1)) // X * 1 == X
2845 return ReplaceInstUsesWith(I, Op0);
2846 if (CI->isAllOnesValue()) // X * -1 == 0 - X
Chris Lattner35236d82003-06-25 17:09:20 +00002847 return BinaryOperator::createNeg(Op0, I.getName());
Chris Lattner31ba1292002-04-29 22:24:47 +00002848
Zhou Sheng4961cf12007-03-29 01:57:21 +00002849 const APInt& Val = cast<ConstantInt>(CI)->getValue();
Reid Spencer6d392062007-03-23 20:05:17 +00002850 if (Val.isPowerOf2()) { // Replace X*(2^C) with X << C
Reid Spencer0d5f9232007-02-02 14:08:20 +00002851 return BinaryOperator::createShl(Op0,
Reid Spencer6d392062007-03-23 20:05:17 +00002852 ConstantInt::get(Op0->getType(), Val.logBase2()));
Chris Lattner22d00a82005-08-02 19:16:58 +00002853 }
Robert Bocchino7b5b86c2004-07-27 21:02:21 +00002854 } else if (ConstantFP *Op1F = dyn_cast<ConstantFP>(Op1)) {
Chris Lattner3082c5a2003-02-18 19:28:33 +00002855 if (Op1F->isNullValue())
2856 return ReplaceInstUsesWith(I, Op1);
Chris Lattner31ba1292002-04-29 22:24:47 +00002857
Chris Lattner3082c5a2003-02-18 19:28:33 +00002858 // "In IEEE floating point, x*1 is not equivalent to x for nans. However,
2859 // ANSI says we can drop signals, so we can do this anyway." (from GCC)
Dale Johannesen98d3a082007-09-14 22:26:36 +00002860 // We need a better interface for long double here.
2861 if (Op1->getType() == Type::FloatTy || Op1->getType() == Type::DoubleTy)
2862 if (Op1F->isExactlyValue(1.0))
2863 return ReplaceInstUsesWith(I, Op0); // Eliminate 'mul double %X, 1.0'
Chris Lattner3082c5a2003-02-18 19:28:33 +00002864 }
Chris Lattner32c01df2006-03-04 06:04:02 +00002865
2866 if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0))
2867 if (Op0I->getOpcode() == Instruction::Add && Op0I->hasOneUse() &&
2868 isa<ConstantInt>(Op0I->getOperand(1))) {
2869 // Canonicalize (X+C1)*C2 -> X*C2+C1*C2.
2870 Instruction *Add = BinaryOperator::createMul(Op0I->getOperand(0),
2871 Op1, "tmp");
2872 InsertNewInstBefore(Add, I);
2873 Value *C1C2 = ConstantExpr::getMul(Op1,
2874 cast<Constant>(Op0I->getOperand(1)));
2875 return BinaryOperator::createAdd(Add, C1C2);
2876
2877 }
Chris Lattner183b3362004-04-09 19:05:30 +00002878
2879 // Try to fold constant mul into select arguments.
2880 if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
Chris Lattner86102b82005-01-01 16:22:27 +00002881 if (Instruction *R = FoldOpIntoSelect(I, SI, this))
Chris Lattner183b3362004-04-09 19:05:30 +00002882 return R;
Chris Lattner6a4adcd2004-09-29 05:07:12 +00002883
2884 if (isa<PHINode>(Op0))
2885 if (Instruction *NV = FoldOpIntoPhi(I))
2886 return NV;
Chris Lattner260ab202002-04-18 17:39:14 +00002887 }
2888
Chris Lattner934a64cf2003-03-10 23:23:04 +00002889 if (Value *Op0v = dyn_castNegVal(Op0)) // -X * -Y = X*Y
2890 if (Value *Op1v = dyn_castNegVal(I.getOperand(1)))
Chris Lattnerdf20a4d2004-06-10 02:07:29 +00002891 return BinaryOperator::createMul(Op0v, Op1v);
Chris Lattner934a64cf2003-03-10 23:23:04 +00002892
Chris Lattner2635b522004-02-23 05:39:21 +00002893 // If one of the operands of the multiply is a cast from a boolean value, then
2894 // we know the bool is either zero or one, so this is a 'masking' multiply.
2895 // See if we can simplify things based on how the boolean was originally
2896 // formed.
2897 CastInst *BoolCast = 0;
Reid Spencer74a528b2006-12-13 18:21:21 +00002898 if (ZExtInst *CI = dyn_cast<ZExtInst>(I.getOperand(0)))
Reid Spencer542964f2007-01-11 18:21:29 +00002899 if (CI->getOperand(0)->getType() == Type::Int1Ty)
Chris Lattner2635b522004-02-23 05:39:21 +00002900 BoolCast = CI;
2901 if (!BoolCast)
Reid Spencer74a528b2006-12-13 18:21:21 +00002902 if (ZExtInst *CI = dyn_cast<ZExtInst>(I.getOperand(1)))
Reid Spencer542964f2007-01-11 18:21:29 +00002903 if (CI->getOperand(0)->getType() == Type::Int1Ty)
Chris Lattner2635b522004-02-23 05:39:21 +00002904 BoolCast = CI;
2905 if (BoolCast) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002906 if (ICmpInst *SCI = dyn_cast<ICmpInst>(BoolCast->getOperand(0))) {
Chris Lattner2635b522004-02-23 05:39:21 +00002907 Value *SCIOp0 = SCI->getOperand(0), *SCIOp1 = SCI->getOperand(1);
2908 const Type *SCOpTy = SCIOp0->getType();
Chris Lattner06205d52007-07-15 20:42:37 +00002909 bool TIS = false;
2910
Reid Spencer266e42b2006-12-23 06:05:41 +00002911 // If the icmp is true iff the sign bit of X is set, then convert this
Chris Lattnere79e8542004-02-23 06:38:22 +00002912 // multiply into a shift/and combination.
2913 if (isa<ConstantInt>(SCIOp1) &&
Chris Lattner06205d52007-07-15 20:42:37 +00002914 isSignBitCheck(SCI->getPredicate(), cast<ConstantInt>(SCIOp1), TIS) &&
2915 TIS) {
Chris Lattner2635b522004-02-23 05:39:21 +00002916 // Shift the X value right to turn it into "all signbits".
Reid Spencer2341c222007-02-02 02:16:23 +00002917 Constant *Amt = ConstantInt::get(SCIOp0->getType(),
Chris Lattnerd1f46d32005-04-24 06:59:08 +00002918 SCOpTy->getPrimitiveSizeInBits()-1);
Chris Lattnere79e8542004-02-23 06:38:22 +00002919 Value *V =
Reid Spencer2341c222007-02-02 02:16:23 +00002920 InsertNewInstBefore(
2921 BinaryOperator::create(Instruction::AShr, SCIOp0, Amt,
Chris Lattnere79e8542004-02-23 06:38:22 +00002922 BoolCast->getOperand(0)->getName()+
2923 ".mask"), I);
Chris Lattner2635b522004-02-23 05:39:21 +00002924
2925 // If the multiply type is not the same as the source type, sign extend
2926 // or truncate to the multiply type.
Reid Spencer13bc5d72006-12-12 09:18:51 +00002927 if (I.getType() != V->getType()) {
Zhou Sheng56cda952007-04-02 08:20:41 +00002928 uint32_t SrcBits = V->getType()->getPrimitiveSizeInBits();
2929 uint32_t DstBits = I.getType()->getPrimitiveSizeInBits();
Reid Spencer13bc5d72006-12-12 09:18:51 +00002930 Instruction::CastOps opcode =
2931 (SrcBits == DstBits ? Instruction::BitCast :
2932 (SrcBits < DstBits ? Instruction::SExt : Instruction::Trunc));
2933 V = InsertCastBefore(opcode, V, I.getType(), I);
2934 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00002935
Chris Lattner2635b522004-02-23 05:39:21 +00002936 Value *OtherOp = Op0 == BoolCast ? I.getOperand(1) : Op0;
Chris Lattnerdf20a4d2004-06-10 02:07:29 +00002937 return BinaryOperator::createAnd(V, OtherOp);
Chris Lattner2635b522004-02-23 05:39:21 +00002938 }
2939 }
2940 }
2941
Chris Lattner113f4f42002-06-25 16:13:24 +00002942 return Changed ? &I : 0;
Chris Lattner260ab202002-04-18 17:39:14 +00002943}
2944
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002945/// This function implements the transforms on div instructions that work
2946/// regardless of the kind of div instruction it is (udiv, sdiv, or fdiv). It is
2947/// used by the visitors to those instructions.
2948/// @brief Transforms common to all three div instructions
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002949Instruction *InstCombiner::commonDivTransforms(BinaryOperator &I) {
Chris Lattnerbf5b7cf2004-12-12 21:48:58 +00002950 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
Chris Lattner81a7a232004-10-16 18:11:37 +00002951
Chris Lattner0fe6bce2008-02-19 06:12:18 +00002952 // undef / X -> 0 for integer.
2953 // undef / X -> undef for FP (the undef could be a snan).
2954 if (isa<UndefValue>(Op0)) {
2955 if (Op0->getType()->isFPOrFPVector())
2956 return ReplaceInstUsesWith(I, Op0);
Chris Lattnerbf5b7cf2004-12-12 21:48:58 +00002957 return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
Chris Lattner0fe6bce2008-02-19 06:12:18 +00002958 }
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002959
2960 // X / undef -> undef
Chris Lattnerbf5b7cf2004-12-12 21:48:58 +00002961 if (isa<UndefValue>(Op1))
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002962 return ReplaceInstUsesWith(I, Op1);
Chris Lattnerbf5b7cf2004-12-12 21:48:58 +00002963
Chris Lattner710b4412008-01-28 00:58:18 +00002964 // Handle cases involving: [su]div X, (select Cond, Y, Z)
2965 // This does not apply for fdiv.
Chris Lattnerd79dc792006-09-09 20:26:32 +00002966 if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) {
Chris Lattner710b4412008-01-28 00:58:18 +00002967 // [su]div X, (Cond ? 0 : Y) -> div X, Y. If the div and the select are in
2968 // the same basic block, then we replace the select with Y, and the
2969 // condition of the select with false (if the cond value is in the same BB).
2970 // If the select has uses other than the div, this allows them to be
2971 // simplified also. Note that div X, Y is just as good as div X, 0 (undef)
2972 if (ConstantInt *ST = dyn_cast<ConstantInt>(SI->getOperand(1)))
Chris Lattnerd79dc792006-09-09 20:26:32 +00002973 if (ST->isNullValue()) {
2974 Instruction *CondI = dyn_cast<Instruction>(SI->getOperand(0));
2975 if (CondI && CondI->getParent() == I.getParent())
Zhou Sheng75b871f2007-01-11 12:24:14 +00002976 UpdateValueUsesWith(CondI, ConstantInt::getFalse());
Chris Lattnerd79dc792006-09-09 20:26:32 +00002977 else if (I.getParent() != SI->getParent() || SI->hasOneUse())
2978 I.setOperand(1, SI->getOperand(2));
2979 else
2980 UpdateValueUsesWith(SI, SI->getOperand(2));
2981 return &I;
2982 }
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002983
Chris Lattner710b4412008-01-28 00:58:18 +00002984 // Likewise for: [su]div X, (Cond ? Y : 0) -> div X, Y
2985 if (ConstantInt *ST = dyn_cast<ConstantInt>(SI->getOperand(2)))
Chris Lattnerd79dc792006-09-09 20:26:32 +00002986 if (ST->isNullValue()) {
2987 Instruction *CondI = dyn_cast<Instruction>(SI->getOperand(0));
2988 if (CondI && CondI->getParent() == I.getParent())
Zhou Sheng75b871f2007-01-11 12:24:14 +00002989 UpdateValueUsesWith(CondI, ConstantInt::getTrue());
Chris Lattnerd79dc792006-09-09 20:26:32 +00002990 else if (I.getParent() != SI->getParent() || SI->hasOneUse())
2991 I.setOperand(1, SI->getOperand(1));
2992 else
2993 UpdateValueUsesWith(SI, SI->getOperand(1));
2994 return &I;
2995 }
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002996 }
Chris Lattnerd79dc792006-09-09 20:26:32 +00002997
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002998 return 0;
2999}
Misha Brukmanb1c93172005-04-21 23:48:37 +00003000
Reid Spencer7e80b0b2006-10-26 06:15:43 +00003001/// This function implements the transforms common to both integer division
3002/// instructions (udiv and sdiv). It is called by the visitors to those integer
3003/// division instructions.
3004/// @brief Common integer divide transforms
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003005Instruction *InstCombiner::commonIDivTransforms(BinaryOperator &I) {
Reid Spencer7e80b0b2006-10-26 06:15:43 +00003006 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
3007
3008 if (Instruction *Common = commonDivTransforms(I))
3009 return Common;
3010
3011 if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) {
3012 // div X, 1 == X
3013 if (RHS->equalsInt(1))
3014 return ReplaceInstUsesWith(I, Op0);
3015
3016 // (X / C1) / C2 -> X / (C1*C2)
3017 if (Instruction *LHS = dyn_cast<Instruction>(Op0))
3018 if (Instruction::BinaryOps(LHS->getOpcode()) == I.getOpcode())
3019 if (ConstantInt *LHSRHS = dyn_cast<ConstantInt>(LHS->getOperand(1))) {
Nick Lewyckyfefd0202008-02-18 22:48:05 +00003020 if (MultiplyOverflows(RHS, LHSRHS, I.getOpcode()==Instruction::SDiv))
3021 return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
3022 else
3023 return BinaryOperator::create(I.getOpcode(), LHS->getOperand(0),
3024 Multiply(RHS, LHSRHS));
Chris Lattner42362612005-04-08 04:03:26 +00003025 }
Reid Spencer7e80b0b2006-10-26 06:15:43 +00003026
Reid Spencer6d392062007-03-23 20:05:17 +00003027 if (!RHS->isZero()) { // avoid X udiv 0
Reid Spencer7e80b0b2006-10-26 06:15:43 +00003028 if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
3029 if (Instruction *R = FoldOpIntoSelect(I, SI, this))
3030 return R;
3031 if (isa<PHINode>(Op0))
3032 if (Instruction *NV = FoldOpIntoPhi(I))
3033 return NV;
3034 }
Chris Lattnerd79dc792006-09-09 20:26:32 +00003035 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00003036
Chris Lattner3082c5a2003-02-18 19:28:33 +00003037 // 0 / X == 0, we don't need to preserve faults!
Chris Lattnerbf5b7cf2004-12-12 21:48:58 +00003038 if (ConstantInt *LHS = dyn_cast<ConstantInt>(Op0))
Chris Lattner3082c5a2003-02-18 19:28:33 +00003039 if (LHS->equalsInt(0))
3040 return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
3041
Reid Spencer7e80b0b2006-10-26 06:15:43 +00003042 return 0;
3043}
3044
3045Instruction *InstCombiner::visitUDiv(BinaryOperator &I) {
3046 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
3047
3048 // Handle the integer div common cases
3049 if (Instruction *Common = commonIDivTransforms(I))
3050 return Common;
3051
3052 // X udiv C^2 -> X >> C
3053 // Check to see if this is an unsigned division with an exact power of 2,
3054 // if so, convert to a right shift.
3055 if (ConstantInt *C = dyn_cast<ConstantInt>(Op1)) {
Reid Spencer54d5b1b2007-03-26 23:58:26 +00003056 if (C->getValue().isPowerOf2()) // 0 not included in isPowerOf2
Reid Spencer6d392062007-03-23 20:05:17 +00003057 return BinaryOperator::createLShr(Op0,
Zhou Sheng222d5eb2007-03-25 05:01:29 +00003058 ConstantInt::get(Op0->getType(), C->getValue().logBase2()));
Reid Spencer7e80b0b2006-10-26 06:15:43 +00003059 }
3060
3061 // X udiv (C1 << N), where C1 is "1<<C2" --> X >> (N+C2)
Reid Spencer2341c222007-02-02 02:16:23 +00003062 if (BinaryOperator *RHSI = dyn_cast<BinaryOperator>(I.getOperand(1))) {
Reid Spencer7e80b0b2006-10-26 06:15:43 +00003063 if (RHSI->getOpcode() == Instruction::Shl &&
3064 isa<ConstantInt>(RHSI->getOperand(0))) {
Zhou Sheng150f3bb2007-04-01 17:13:37 +00003065 const APInt& C1 = cast<ConstantInt>(RHSI->getOperand(0))->getValue();
Reid Spencer6d392062007-03-23 20:05:17 +00003066 if (C1.isPowerOf2()) {
Reid Spencer7e80b0b2006-10-26 06:15:43 +00003067 Value *N = RHSI->getOperand(1);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003068 const Type *NTy = N->getType();
Reid Spencer959a21d2007-03-23 21:24:59 +00003069 if (uint32_t C2 = C1.logBase2()) {
Reid Spencer7e80b0b2006-10-26 06:15:43 +00003070 Constant *C2V = ConstantInt::get(NTy, C2);
3071 N = InsertNewInstBefore(BinaryOperator::createAdd(N, C2V, "tmp"), I);
Chris Lattner2e90b732006-02-05 07:54:04 +00003072 }
Reid Spencer0d5f9232007-02-02 14:08:20 +00003073 return BinaryOperator::createLShr(Op0, N);
Chris Lattner2e90b732006-02-05 07:54:04 +00003074 }
3075 }
Chris Lattnerdd0c1742005-11-05 07:40:31 +00003076 }
3077
Reid Spencer7e80b0b2006-10-26 06:15:43 +00003078 // udiv X, (Select Cond, C1, C2) --> Select Cond, (shr X, C1), (shr X, C2)
3079 // where C1&C2 are powers of two.
Reid Spencer3939b1a2007-03-05 23:36:13 +00003080 if (SelectInst *SI = dyn_cast<SelectInst>(Op1))
Reid Spencer7e80b0b2006-10-26 06:15:43 +00003081 if (ConstantInt *STO = dyn_cast<ConstantInt>(SI->getOperand(1)))
Reid Spencer3939b1a2007-03-05 23:36:13 +00003082 if (ConstantInt *SFO = dyn_cast<ConstantInt>(SI->getOperand(2))) {
Zhou Sheng150f3bb2007-04-01 17:13:37 +00003083 const APInt &TVA = STO->getValue(), &FVA = SFO->getValue();
Reid Spencer6d392062007-03-23 20:05:17 +00003084 if (TVA.isPowerOf2() && FVA.isPowerOf2()) {
Reid Spencer3939b1a2007-03-05 23:36:13 +00003085 // Compute the shift amounts
Reid Spencer6d392062007-03-23 20:05:17 +00003086 uint32_t TSA = TVA.logBase2(), FSA = FVA.logBase2();
Reid Spencer3939b1a2007-03-05 23:36:13 +00003087 // Construct the "on true" case of the select
3088 Constant *TC = ConstantInt::get(Op0->getType(), TSA);
3089 Instruction *TSI = BinaryOperator::createLShr(
3090 Op0, TC, SI->getName()+".t");
3091 TSI = InsertNewInstBefore(TSI, I);
3092
3093 // Construct the "on false" case of the select
3094 Constant *FC = ConstantInt::get(Op0->getType(), FSA);
3095 Instruction *FSI = BinaryOperator::createLShr(
3096 Op0, FC, SI->getName()+".f");
3097 FSI = InsertNewInstBefore(FSI, I);
Reid Spencer7e80b0b2006-10-26 06:15:43 +00003098
Reid Spencer3939b1a2007-03-05 23:36:13 +00003099 // construct the select instruction and return it.
Gabor Greife9ecc682008-04-06 20:25:17 +00003100 return SelectInst::Create(SI->getOperand(0), TSI, FSI, SI->getName());
Reid Spencer7e80b0b2006-10-26 06:15:43 +00003101 }
Reid Spencer3939b1a2007-03-05 23:36:13 +00003102 }
Chris Lattnerf4cdbf32002-05-06 16:14:14 +00003103 return 0;
3104}
3105
Reid Spencer7e80b0b2006-10-26 06:15:43 +00003106Instruction *InstCombiner::visitSDiv(BinaryOperator &I) {
3107 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
3108
3109 // Handle the integer div common cases
3110 if (Instruction *Common = commonIDivTransforms(I))
3111 return Common;
3112
3113 if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) {
3114 // sdiv X, -1 == -X
3115 if (RHS->isAllOnesValue())
3116 return BinaryOperator::createNeg(Op0);
3117
3118 // -X/C -> X/-C
3119 if (Value *LHSNeg = dyn_castNegVal(Op0))
3120 return BinaryOperator::createSDiv(LHSNeg, ConstantExpr::getNeg(RHS));
3121 }
3122
3123 // If the sign bits of both operands are zero (i.e. we can prove they are
3124 // unsigned inputs), turn this into a udiv.
Chris Lattner03c49532007-01-15 02:27:26 +00003125 if (I.getType()->isInteger()) {
Reid Spencer6d392062007-03-23 20:05:17 +00003126 APInt Mask(APInt::getSignBit(I.getType()->getPrimitiveSizeInBits()));
Reid Spencer7e80b0b2006-10-26 06:15:43 +00003127 if (MaskedValueIsZero(Op1, Mask) && MaskedValueIsZero(Op0, Mask)) {
Dan Gohman4decbc52007-11-05 23:16:33 +00003128 // X sdiv Y -> X udiv Y, iff X and Y don't have sign bit set
Reid Spencer7e80b0b2006-10-26 06:15:43 +00003129 return BinaryOperator::createUDiv(Op0, Op1, I.getName());
3130 }
3131 }
3132
3133 return 0;
3134}
3135
3136Instruction *InstCombiner::visitFDiv(BinaryOperator &I) {
3137 return commonDivTransforms(I);
3138}
Chris Lattnerf4cdbf32002-05-06 16:14:14 +00003139
Reid Spencer7eb55b32006-11-02 01:53:59 +00003140/// This function implements the transforms on rem instructions that work
3141/// regardless of the kind of rem instruction it is (urem, srem, or frem). It
3142/// is used by the visitors to those instructions.
3143/// @brief Transforms common to all three rem instructions
3144Instruction *InstCombiner::commonRemTransforms(BinaryOperator &I) {
Chris Lattnerbf5b7cf2004-12-12 21:48:58 +00003145 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
Reid Spencer7eb55b32006-11-02 01:53:59 +00003146
Chris Lattner0fe6bce2008-02-19 06:12:18 +00003147 // 0 % X == 0 for integer, we don't need to preserve faults!
Chris Lattner0de4a8d2006-02-28 05:30:45 +00003148 if (Constant *LHS = dyn_cast<Constant>(Op0))
3149 if (LHS->isNullValue())
3150 return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
3151
Chris Lattner0fe6bce2008-02-19 06:12:18 +00003152 if (isa<UndefValue>(Op0)) { // undef % X -> 0
3153 if (I.getType()->isFPOrFPVector())
3154 return ReplaceInstUsesWith(I, Op0); // X % undef -> undef (could be SNaN)
Chris Lattner0de4a8d2006-02-28 05:30:45 +00003155 return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
Chris Lattner0fe6bce2008-02-19 06:12:18 +00003156 }
Chris Lattner0de4a8d2006-02-28 05:30:45 +00003157 if (isa<UndefValue>(Op1))
3158 return ReplaceInstUsesWith(I, Op1); // X % undef -> undef
Reid Spencer7eb55b32006-11-02 01:53:59 +00003159
3160 // Handle cases involving: rem X, (select Cond, Y, Z)
3161 if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) {
3162 // rem X, (Cond ? 0 : Y) -> rem X, Y. If the rem and the select are in
3163 // the same basic block, then we replace the select with Y, and the
3164 // condition of the select with false (if the cond value is in the same
3165 // BB). If the select has uses other than the div, this allows them to be
3166 // simplified also.
3167 if (Constant *ST = dyn_cast<Constant>(SI->getOperand(1)))
3168 if (ST->isNullValue()) {
3169 Instruction *CondI = dyn_cast<Instruction>(SI->getOperand(0));
3170 if (CondI && CondI->getParent() == I.getParent())
Zhou Sheng75b871f2007-01-11 12:24:14 +00003171 UpdateValueUsesWith(CondI, ConstantInt::getFalse());
Reid Spencer7eb55b32006-11-02 01:53:59 +00003172 else if (I.getParent() != SI->getParent() || SI->hasOneUse())
3173 I.setOperand(1, SI->getOperand(2));
3174 else
3175 UpdateValueUsesWith(SI, SI->getOperand(2));
Chris Lattner7fd5f072004-07-06 07:01:22 +00003176 return &I;
3177 }
Reid Spencer7eb55b32006-11-02 01:53:59 +00003178 // Likewise for: rem X, (Cond ? Y : 0) -> rem X, Y
3179 if (Constant *ST = dyn_cast<Constant>(SI->getOperand(2)))
3180 if (ST->isNullValue()) {
3181 Instruction *CondI = dyn_cast<Instruction>(SI->getOperand(0));
3182 if (CondI && CondI->getParent() == I.getParent())
Zhou Sheng75b871f2007-01-11 12:24:14 +00003183 UpdateValueUsesWith(CondI, ConstantInt::getTrue());
Reid Spencer7eb55b32006-11-02 01:53:59 +00003184 else if (I.getParent() != SI->getParent() || SI->hasOneUse())
3185 I.setOperand(1, SI->getOperand(1));
3186 else
3187 UpdateValueUsesWith(SI, SI->getOperand(1));
3188 return &I;
3189 }
Chris Lattnere9ff0ea2005-11-05 07:28:37 +00003190 }
Chris Lattner7fd5f072004-07-06 07:01:22 +00003191
Reid Spencer7eb55b32006-11-02 01:53:59 +00003192 return 0;
3193}
3194
3195/// This function implements the transforms common to both integer remainder
3196/// instructions (urem and srem). It is called by the visitors to those integer
3197/// remainder instructions.
3198/// @brief Common integer remainder transforms
3199Instruction *InstCombiner::commonIRemTransforms(BinaryOperator &I) {
3200 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
3201
3202 if (Instruction *common = commonRemTransforms(I))
3203 return common;
3204
Chris Lattnerbf5b7cf2004-12-12 21:48:58 +00003205 if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) {
Chris Lattner0de4a8d2006-02-28 05:30:45 +00003206 // X % 0 == undef, we don't need to preserve faults!
3207 if (RHS->equalsInt(0))
3208 return ReplaceInstUsesWith(I, UndefValue::get(I.getType()));
3209
Chris Lattner3082c5a2003-02-18 19:28:33 +00003210 if (RHS->equalsInt(1)) // X % 1 == 0
3211 return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
3212
Chris Lattnerb70f1412006-02-28 05:49:21 +00003213 if (Instruction *Op0I = dyn_cast<Instruction>(Op0)) {
3214 if (SelectInst *SI = dyn_cast<SelectInst>(Op0I)) {
3215 if (Instruction *R = FoldOpIntoSelect(I, SI, this))
3216 return R;
3217 } else if (isa<PHINode>(Op0I)) {
3218 if (Instruction *NV = FoldOpIntoPhi(I))
3219 return NV;
Chris Lattnerb70f1412006-02-28 05:49:21 +00003220 }
Nick Lewyckyd0b62a12008-03-06 06:48:30 +00003221
3222 // See if we can fold away this rem instruction.
3223 uint32_t BitWidth = cast<IntegerType>(I.getType())->getBitWidth();
3224 APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0);
3225 if (SimplifyDemandedBits(&I, APInt::getAllOnesValue(BitWidth),
3226 KnownZero, KnownOne))
3227 return &I;
Chris Lattnerb70f1412006-02-28 05:49:21 +00003228 }
Chris Lattner3082c5a2003-02-18 19:28:33 +00003229 }
3230
Reid Spencer7eb55b32006-11-02 01:53:59 +00003231 return 0;
3232}
3233
3234Instruction *InstCombiner::visitURem(BinaryOperator &I) {
3235 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
3236
3237 if (Instruction *common = commonIRemTransforms(I))
3238 return common;
3239
3240 if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) {
3241 // X urem C^2 -> X and C
3242 // Check to see if this is an unsigned remainder with an exact power of 2,
3243 // if so, convert to a bitwise and.
3244 if (ConstantInt *C = dyn_cast<ConstantInt>(RHS))
Reid Spencer6d392062007-03-23 20:05:17 +00003245 if (C->getValue().isPowerOf2())
Reid Spencer7eb55b32006-11-02 01:53:59 +00003246 return BinaryOperator::createAnd(Op0, SubOne(C));
3247 }
3248
Chris Lattner2e90b732006-02-05 07:54:04 +00003249 if (Instruction *RHSI = dyn_cast<Instruction>(I.getOperand(1))) {
Reid Spencer7eb55b32006-11-02 01:53:59 +00003250 // Turn A % (C << N), where C is 2^k, into A & ((C << N)-1)
3251 if (RHSI->getOpcode() == Instruction::Shl &&
3252 isa<ConstantInt>(RHSI->getOperand(0))) {
Zhou Sheng222d5eb2007-03-25 05:01:29 +00003253 if (cast<ConstantInt>(RHSI->getOperand(0))->getValue().isPowerOf2()) {
Chris Lattner2e90b732006-02-05 07:54:04 +00003254 Constant *N1 = ConstantInt::getAllOnesValue(I.getType());
3255 Value *Add = InsertNewInstBefore(BinaryOperator::createAdd(RHSI, N1,
3256 "tmp"), I);
3257 return BinaryOperator::createAnd(Op0, Add);
3258 }
3259 }
Reid Spencer7eb55b32006-11-02 01:53:59 +00003260 }
Chris Lattnerd79dc792006-09-09 20:26:32 +00003261
Reid Spencer7eb55b32006-11-02 01:53:59 +00003262 // urem X, (select Cond, 2^C1, 2^C2) --> select Cond, (and X, C1), (and X, C2)
3263 // where C1&C2 are powers of two.
3264 if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) {
3265 if (ConstantInt *STO = dyn_cast<ConstantInt>(SI->getOperand(1)))
3266 if (ConstantInt *SFO = dyn_cast<ConstantInt>(SI->getOperand(2))) {
3267 // STO == 0 and SFO == 0 handled above.
Reid Spencer6d392062007-03-23 20:05:17 +00003268 if ((STO->getValue().isPowerOf2()) &&
3269 (SFO->getValue().isPowerOf2())) {
Reid Spencer7eb55b32006-11-02 01:53:59 +00003270 Value *TrueAnd = InsertNewInstBefore(
3271 BinaryOperator::createAnd(Op0, SubOne(STO), SI->getName()+".t"), I);
3272 Value *FalseAnd = InsertNewInstBefore(
3273 BinaryOperator::createAnd(Op0, SubOne(SFO), SI->getName()+".f"), I);
Gabor Greife9ecc682008-04-06 20:25:17 +00003274 return SelectInst::Create(SI->getOperand(0), TrueAnd, FalseAnd);
Reid Spencer7eb55b32006-11-02 01:53:59 +00003275 }
3276 }
Chris Lattner2e90b732006-02-05 07:54:04 +00003277 }
3278
Chris Lattnerf4cdbf32002-05-06 16:14:14 +00003279 return 0;
3280}
3281
Reid Spencer7eb55b32006-11-02 01:53:59 +00003282Instruction *InstCombiner::visitSRem(BinaryOperator &I) {
3283 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
3284
Dan Gohman4decbc52007-11-05 23:16:33 +00003285 // Handle the integer rem common cases
Reid Spencer7eb55b32006-11-02 01:53:59 +00003286 if (Instruction *common = commonIRemTransforms(I))
3287 return common;
3288
3289 if (Value *RHSNeg = dyn_castNegVal(Op1))
3290 if (!isa<ConstantInt>(RHSNeg) ||
Zhou Sheng222d5eb2007-03-25 05:01:29 +00003291 cast<ConstantInt>(RHSNeg)->getValue().isStrictlyPositive()) {
Reid Spencer7eb55b32006-11-02 01:53:59 +00003292 // X % -Y -> X % Y
3293 AddUsesToWorkList(I);
3294 I.setOperand(1, RHSNeg);
3295 return &I;
3296 }
3297
Dan Gohman4decbc52007-11-05 23:16:33 +00003298 // If the sign bits of both operands are zero (i.e. we can prove they are
Reid Spencer7eb55b32006-11-02 01:53:59 +00003299 // unsigned inputs), turn this into a urem.
Dan Gohman4decbc52007-11-05 23:16:33 +00003300 if (I.getType()->isInteger()) {
3301 APInt Mask(APInt::getSignBit(I.getType()->getPrimitiveSizeInBits()));
3302 if (MaskedValueIsZero(Op1, Mask) && MaskedValueIsZero(Op0, Mask)) {
3303 // X srem Y -> X urem Y, iff X and Y don't have sign bit set
3304 return BinaryOperator::createURem(Op0, Op1, I.getName());
3305 }
Reid Spencer7eb55b32006-11-02 01:53:59 +00003306 }
3307
3308 return 0;
3309}
3310
3311Instruction *InstCombiner::visitFRem(BinaryOperator &I) {
Reid Spencer7eb55b32006-11-02 01:53:59 +00003312 return commonRemTransforms(I);
3313}
3314
Chris Lattner6d14f2a2002-08-09 23:47:40 +00003315// isMaxValueMinusOne - return true if this is Max-1
Reid Spencer266e42b2006-12-23 06:05:41 +00003316static bool isMaxValueMinusOne(const ConstantInt *C, bool isSigned) {
Reid Spenceref599b02007-03-19 21:10:28 +00003317 uint32_t TypeBits = C->getType()->getPrimitiveSizeInBits();
Chris Lattner06205d52007-07-15 20:42:37 +00003318 if (!isSigned)
3319 return C->getValue() == APInt::getAllOnesValue(TypeBits) - 1;
3320 return C->getValue() == APInt::getSignedMaxValue(TypeBits)-1;
Chris Lattner6d14f2a2002-08-09 23:47:40 +00003321}
3322
3323// isMinValuePlusOne - return true if this is Min+1
Reid Spencer266e42b2006-12-23 06:05:41 +00003324static bool isMinValuePlusOne(const ConstantInt *C, bool isSigned) {
Chris Lattner06205d52007-07-15 20:42:37 +00003325 if (!isSigned)
3326 return C->getValue() == 1; // unsigned
3327
3328 // Calculate 1111111111000000000000
3329 uint32_t TypeBits = C->getType()->getPrimitiveSizeInBits();
3330 return C->getValue() == APInt::getSignedMinValue(TypeBits)+1;
Chris Lattner6d14f2a2002-08-09 23:47:40 +00003331}
3332
Chris Lattner35167c32004-06-09 07:59:58 +00003333// isOneBitSet - Return true if there is exactly one bit set in the specified
3334// constant.
3335static bool isOneBitSet(const ConstantInt *CI) {
Reid Spencer66827212007-03-20 00:16:52 +00003336 return CI->getValue().isPowerOf2();
Chris Lattner35167c32004-06-09 07:59:58 +00003337}
3338
Chris Lattner8fc5af42004-09-23 21:46:38 +00003339// isHighOnes - Return true if the constant is of the form 1+0+.
3340// This is the same as lowones(~X).
3341static bool isHighOnes(const ConstantInt *CI) {
Zhou Shengb3949342007-03-20 12:49:06 +00003342 return (~CI->getValue() + 1).isPowerOf2();
Chris Lattner8fc5af42004-09-23 21:46:38 +00003343}
3344
Reid Spencer266e42b2006-12-23 06:05:41 +00003345/// getICmpCode - Encode a icmp predicate into a three bit mask. These bits
Chris Lattner3ac7c262003-08-13 20:16:26 +00003346/// are carefully arranged to allow folding of expressions such as:
3347///
3348/// (A < B) | (A > B) --> (A != B)
3349///
Reid Spencer266e42b2006-12-23 06:05:41 +00003350/// Note that this is only valid if the first and second predicates have the
3351/// same sign. Is illegal to do: (A u< B) | (A s> B)
Chris Lattner3ac7c262003-08-13 20:16:26 +00003352///
Reid Spencer266e42b2006-12-23 06:05:41 +00003353/// Three bits are used to represent the condition, as follows:
3354/// 0 A > B
3355/// 1 A == B
3356/// 2 A < B
3357///
3358/// <=> Value Definition
3359/// 000 0 Always false
3360/// 001 1 A > B
3361/// 010 2 A == B
3362/// 011 3 A >= B
3363/// 100 4 A < B
3364/// 101 5 A != B
3365/// 110 6 A <= B
3366/// 111 7 Always true
3367///
3368static unsigned getICmpCode(const ICmpInst *ICI) {
3369 switch (ICI->getPredicate()) {
Chris Lattner3ac7c262003-08-13 20:16:26 +00003370 // False -> 0
Reid Spencer266e42b2006-12-23 06:05:41 +00003371 case ICmpInst::ICMP_UGT: return 1; // 001
3372 case ICmpInst::ICMP_SGT: return 1; // 001
3373 case ICmpInst::ICMP_EQ: return 2; // 010
3374 case ICmpInst::ICMP_UGE: return 3; // 011
3375 case ICmpInst::ICMP_SGE: return 3; // 011
3376 case ICmpInst::ICMP_ULT: return 4; // 100
3377 case ICmpInst::ICMP_SLT: return 4; // 100
3378 case ICmpInst::ICMP_NE: return 5; // 101
3379 case ICmpInst::ICMP_ULE: return 6; // 110
3380 case ICmpInst::ICMP_SLE: return 6; // 110
Chris Lattner3ac7c262003-08-13 20:16:26 +00003381 // True -> 7
3382 default:
Reid Spencer266e42b2006-12-23 06:05:41 +00003383 assert(0 && "Invalid ICmp predicate!");
Chris Lattner3ac7c262003-08-13 20:16:26 +00003384 return 0;
3385 }
3386}
3387
Reid Spencer266e42b2006-12-23 06:05:41 +00003388/// getICmpValue - This is the complement of getICmpCode, which turns an
3389/// opcode and two operands into either a constant true or false, or a brand
Dan Gohman2ac26522007-09-17 17:31:57 +00003390/// new ICmp instruction. The sign is passed in to determine which kind
Reid Spencer266e42b2006-12-23 06:05:41 +00003391/// of predicate to use in new icmp instructions.
3392static Value *getICmpValue(bool sign, unsigned code, Value *LHS, Value *RHS) {
3393 switch (code) {
3394 default: assert(0 && "Illegal ICmp code!");
Zhou Sheng75b871f2007-01-11 12:24:14 +00003395 case 0: return ConstantInt::getFalse();
Reid Spencer266e42b2006-12-23 06:05:41 +00003396 case 1:
3397 if (sign)
3398 return new ICmpInst(ICmpInst::ICMP_SGT, LHS, RHS);
3399 else
3400 return new ICmpInst(ICmpInst::ICMP_UGT, LHS, RHS);
3401 case 2: return new ICmpInst(ICmpInst::ICMP_EQ, LHS, RHS);
3402 case 3:
3403 if (sign)
3404 return new ICmpInst(ICmpInst::ICMP_SGE, LHS, RHS);
3405 else
3406 return new ICmpInst(ICmpInst::ICMP_UGE, LHS, RHS);
3407 case 4:
3408 if (sign)
3409 return new ICmpInst(ICmpInst::ICMP_SLT, LHS, RHS);
3410 else
3411 return new ICmpInst(ICmpInst::ICMP_ULT, LHS, RHS);
3412 case 5: return new ICmpInst(ICmpInst::ICMP_NE, LHS, RHS);
3413 case 6:
3414 if (sign)
3415 return new ICmpInst(ICmpInst::ICMP_SLE, LHS, RHS);
3416 else
3417 return new ICmpInst(ICmpInst::ICMP_ULE, LHS, RHS);
Zhou Sheng75b871f2007-01-11 12:24:14 +00003418 case 7: return ConstantInt::getTrue();
Chris Lattner3ac7c262003-08-13 20:16:26 +00003419 }
3420}
3421
Reid Spencer266e42b2006-12-23 06:05:41 +00003422static bool PredicatesFoldable(ICmpInst::Predicate p1, ICmpInst::Predicate p2) {
3423 return (ICmpInst::isSignedPredicate(p1) == ICmpInst::isSignedPredicate(p2)) ||
3424 (ICmpInst::isSignedPredicate(p1) &&
3425 (p2 == ICmpInst::ICMP_EQ || p2 == ICmpInst::ICMP_NE)) ||
3426 (ICmpInst::isSignedPredicate(p2) &&
3427 (p1 == ICmpInst::ICMP_EQ || p1 == ICmpInst::ICMP_NE));
3428}
3429
3430namespace {
3431// FoldICmpLogical - Implements (icmp1 A, B) & (icmp2 A, B) --> (icmp3 A, B)
3432struct FoldICmpLogical {
Chris Lattner3ac7c262003-08-13 20:16:26 +00003433 InstCombiner &IC;
3434 Value *LHS, *RHS;
Reid Spencer266e42b2006-12-23 06:05:41 +00003435 ICmpInst::Predicate pred;
3436 FoldICmpLogical(InstCombiner &ic, ICmpInst *ICI)
3437 : IC(ic), LHS(ICI->getOperand(0)), RHS(ICI->getOperand(1)),
3438 pred(ICI->getPredicate()) {}
Chris Lattner3ac7c262003-08-13 20:16:26 +00003439 bool shouldApply(Value *V) const {
Reid Spencer266e42b2006-12-23 06:05:41 +00003440 if (ICmpInst *ICI = dyn_cast<ICmpInst>(V))
3441 if (PredicatesFoldable(pred, ICI->getPredicate()))
Anton Korobeynikov1bfd1212008-02-20 11:26:25 +00003442 return ((ICI->getOperand(0) == LHS && ICI->getOperand(1) == RHS) ||
3443 (ICI->getOperand(0) == RHS && ICI->getOperand(1) == LHS));
Chris Lattner3ac7c262003-08-13 20:16:26 +00003444 return false;
3445 }
Reid Spencer266e42b2006-12-23 06:05:41 +00003446 Instruction *apply(Instruction &Log) const {
3447 ICmpInst *ICI = cast<ICmpInst>(Log.getOperand(0));
3448 if (ICI->getOperand(0) != LHS) {
3449 assert(ICI->getOperand(1) == LHS);
3450 ICI->swapOperands(); // Swap the LHS and RHS of the ICmp
Chris Lattner3ac7c262003-08-13 20:16:26 +00003451 }
3452
Chris Lattnerd1bce952007-03-13 14:27:42 +00003453 ICmpInst *RHSICI = cast<ICmpInst>(Log.getOperand(1));
Reid Spencer266e42b2006-12-23 06:05:41 +00003454 unsigned LHSCode = getICmpCode(ICI);
Chris Lattnerd1bce952007-03-13 14:27:42 +00003455 unsigned RHSCode = getICmpCode(RHSICI);
Chris Lattner3ac7c262003-08-13 20:16:26 +00003456 unsigned Code;
3457 switch (Log.getOpcode()) {
3458 case Instruction::And: Code = LHSCode & RHSCode; break;
3459 case Instruction::Or: Code = LHSCode | RHSCode; break;
3460 case Instruction::Xor: Code = LHSCode ^ RHSCode; break;
Chris Lattner2caaaba2003-09-22 20:33:34 +00003461 default: assert(0 && "Illegal logical opcode!"); return 0;
Chris Lattner3ac7c262003-08-13 20:16:26 +00003462 }
3463
Chris Lattnerd1bce952007-03-13 14:27:42 +00003464 bool isSigned = ICmpInst::isSignedPredicate(RHSICI->getPredicate()) ||
3465 ICmpInst::isSignedPredicate(ICI->getPredicate());
3466
3467 Value *RV = getICmpValue(isSigned, Code, LHS, RHS);
Chris Lattner3ac7c262003-08-13 20:16:26 +00003468 if (Instruction *I = dyn_cast<Instruction>(RV))
3469 return I;
3470 // Otherwise, it's a constant boolean value...
3471 return IC.ReplaceInstUsesWith(Log, RV);
3472 }
3473};
Chris Lattnere3a63d12006-11-15 04:53:24 +00003474} // end anonymous namespace
Chris Lattner3ac7c262003-08-13 20:16:26 +00003475
Chris Lattnerba1cb382003-09-19 17:17:26 +00003476// OptAndOp - This handles expressions of the form ((val OP C1) & C2). Where
3477// the Op parameter is 'OP', OpRHS is 'C1', and AndRHS is 'C2'. Op is
Reid Spencer2341c222007-02-02 02:16:23 +00003478// guaranteed to be a binary operator.
Chris Lattnerba1cb382003-09-19 17:17:26 +00003479Instruction *InstCombiner::OptAndOp(Instruction *Op,
Zhou Sheng75b871f2007-01-11 12:24:14 +00003480 ConstantInt *OpRHS,
3481 ConstantInt *AndRHS,
Chris Lattnerba1cb382003-09-19 17:17:26 +00003482 BinaryOperator &TheAnd) {
3483 Value *X = Op->getOperand(0);
Chris Lattnerfcf21a72004-01-12 19:47:05 +00003484 Constant *Together = 0;
Reid Spencer2341c222007-02-02 02:16:23 +00003485 if (!Op->isShift())
Reid Spencer80263aa2007-03-25 05:33:51 +00003486 Together = And(AndRHS, OpRHS);
Chris Lattnerc1e7cc02004-01-12 19:35:11 +00003487
Chris Lattnerba1cb382003-09-19 17:17:26 +00003488 switch (Op->getOpcode()) {
3489 case Instruction::Xor:
Chris Lattner86102b82005-01-01 16:22:27 +00003490 if (Op->hasOneUse()) {
Chris Lattnerba1cb382003-09-19 17:17:26 +00003491 // (X ^ C1) & C2 --> (X & C2) ^ (C1&C2)
Chris Lattner6e0123b2007-02-11 01:23:03 +00003492 Instruction *And = BinaryOperator::createAnd(X, AndRHS);
Chris Lattnerba1cb382003-09-19 17:17:26 +00003493 InsertNewInstBefore(And, TheAnd);
Chris Lattner6e0123b2007-02-11 01:23:03 +00003494 And->takeName(Op);
Chris Lattnerdf20a4d2004-06-10 02:07:29 +00003495 return BinaryOperator::createXor(And, Together);
Chris Lattnerba1cb382003-09-19 17:17:26 +00003496 }
3497 break;
3498 case Instruction::Or:
Chris Lattner86102b82005-01-01 16:22:27 +00003499 if (Together == AndRHS) // (X | C) & C --> C
3500 return ReplaceInstUsesWith(TheAnd, AndRHS);
Misha Brukmanb1c93172005-04-21 23:48:37 +00003501
Chris Lattner86102b82005-01-01 16:22:27 +00003502 if (Op->hasOneUse() && Together != OpRHS) {
3503 // (X | C1) & C2 --> (X | (C1&C2)) & C2
Chris Lattner6e0123b2007-02-11 01:23:03 +00003504 Instruction *Or = BinaryOperator::createOr(X, Together);
Chris Lattner86102b82005-01-01 16:22:27 +00003505 InsertNewInstBefore(Or, TheAnd);
Chris Lattner6e0123b2007-02-11 01:23:03 +00003506 Or->takeName(Op);
Chris Lattner86102b82005-01-01 16:22:27 +00003507 return BinaryOperator::createAnd(Or, AndRHS);
Chris Lattnerba1cb382003-09-19 17:17:26 +00003508 }
3509 break;
3510 case Instruction::Add:
Chris Lattnerf95d9b92003-10-15 16:48:29 +00003511 if (Op->hasOneUse()) {
Chris Lattnerba1cb382003-09-19 17:17:26 +00003512 // Adding a one to a single bit bit-field should be turned into an XOR
3513 // of the bit. First thing to check is to see if this AND is with a
3514 // single bit constant.
Zhou Sheng150f3bb2007-04-01 17:13:37 +00003515 const APInt& AndRHSV = cast<ConstantInt>(AndRHS)->getValue();
Chris Lattnerba1cb382003-09-19 17:17:26 +00003516
3517 // If there is only one bit set...
Chris Lattner35167c32004-06-09 07:59:58 +00003518 if (isOneBitSet(cast<ConstantInt>(AndRHS))) {
Chris Lattnerba1cb382003-09-19 17:17:26 +00003519 // Ok, at this point, we know that we are masking the result of the
3520 // ADD down to exactly one bit. If the constant we are adding has
3521 // no bits set below this bit, then we can eliminate the ADD.
Zhou Sheng150f3bb2007-04-01 17:13:37 +00003522 const APInt& AddRHS = cast<ConstantInt>(OpRHS)->getValue();
Misha Brukmanb1c93172005-04-21 23:48:37 +00003523
Chris Lattnerba1cb382003-09-19 17:17:26 +00003524 // Check to see if any bits below the one bit set in AndRHSV are set.
3525 if ((AddRHS & (AndRHSV-1)) == 0) {
3526 // If not, the only thing that can effect the output of the AND is
3527 // the bit specified by AndRHSV. If that bit is set, the effect of
3528 // the XOR is to toggle the bit. If it is clear, then the ADD has
3529 // no effect.
3530 if ((AddRHS & AndRHSV) == 0) { // Bit is not set, noop
3531 TheAnd.setOperand(0, X);
3532 return &TheAnd;
3533 } else {
Chris Lattnerba1cb382003-09-19 17:17:26 +00003534 // Pull the XOR out of the AND.
Chris Lattner6e0123b2007-02-11 01:23:03 +00003535 Instruction *NewAnd = BinaryOperator::createAnd(X, AndRHS);
Chris Lattnerba1cb382003-09-19 17:17:26 +00003536 InsertNewInstBefore(NewAnd, TheAnd);
Chris Lattner6e0123b2007-02-11 01:23:03 +00003537 NewAnd->takeName(Op);
Chris Lattnerdf20a4d2004-06-10 02:07:29 +00003538 return BinaryOperator::createXor(NewAnd, AndRHS);
Chris Lattnerba1cb382003-09-19 17:17:26 +00003539 }
3540 }
3541 }
3542 }
3543 break;
Chris Lattner2da29172003-09-19 19:05:02 +00003544
3545 case Instruction::Shl: {
3546 // We know that the AND will not produce any of the bits shifted in, so if
3547 // the anded constant includes them, clear them now!
3548 //
Zhou Shengb3a80b12007-03-29 08:15:12 +00003549 uint32_t BitWidth = AndRHS->getType()->getBitWidth();
Zhou Shengb25806f2007-03-30 09:29:48 +00003550 uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth);
Zhou Shengb3a80b12007-03-29 08:15:12 +00003551 APInt ShlMask(APInt::getHighBitsSet(BitWidth, BitWidth-OpRHSVal));
3552 ConstantInt *CI = ConstantInt::get(AndRHS->getValue() & ShlMask);
Misha Brukmanb1c93172005-04-21 23:48:37 +00003553
Zhou Shengb3a80b12007-03-29 08:15:12 +00003554 if (CI->getValue() == ShlMask) {
3555 // Masking out bits that the shift already masks
Chris Lattner7e794272004-09-24 15:21:34 +00003556 return ReplaceInstUsesWith(TheAnd, Op); // No need for the and.
3557 } else if (CI != AndRHS) { // Reducing bits set in and.
Chris Lattner2da29172003-09-19 19:05:02 +00003558 TheAnd.setOperand(1, CI);
3559 return &TheAnd;
3560 }
3561 break;
Misha Brukmanb1c93172005-04-21 23:48:37 +00003562 }
Reid Spencerfdff9382006-11-08 06:47:33 +00003563 case Instruction::LShr:
3564 {
Chris Lattner2da29172003-09-19 19:05:02 +00003565 // We know that the AND will not produce any of the bits shifted in, so if
3566 // the anded constant includes them, clear them now! This only applies to
3567 // unsigned shifts, because a signed shr may bring in set bits!
3568 //
Zhou Shengb3a80b12007-03-29 08:15:12 +00003569 uint32_t BitWidth = AndRHS->getType()->getBitWidth();
Zhou Shengb25806f2007-03-30 09:29:48 +00003570 uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth);
Zhou Shengb3a80b12007-03-29 08:15:12 +00003571 APInt ShrMask(APInt::getLowBitsSet(BitWidth, BitWidth - OpRHSVal));
3572 ConstantInt *CI = ConstantInt::get(AndRHS->getValue() & ShrMask);
Chris Lattner7e794272004-09-24 15:21:34 +00003573
Zhou Shengb3a80b12007-03-29 08:15:12 +00003574 if (CI->getValue() == ShrMask) {
3575 // Masking out bits that the shift already masks.
Reid Spencerfdff9382006-11-08 06:47:33 +00003576 return ReplaceInstUsesWith(TheAnd, Op);
3577 } else if (CI != AndRHS) {
3578 TheAnd.setOperand(1, CI); // Reduce bits set in and cst.
3579 return &TheAnd;
3580 }
3581 break;
3582 }
3583 case Instruction::AShr:
3584 // Signed shr.
3585 // See if this is shifting in some sign extension, then masking it out
3586 // with an and.
3587 if (Op->hasOneUse()) {
Zhou Shengb3a80b12007-03-29 08:15:12 +00003588 uint32_t BitWidth = AndRHS->getType()->getBitWidth();
Zhou Shengb25806f2007-03-30 09:29:48 +00003589 uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth);
Zhou Shengb3a80b12007-03-29 08:15:12 +00003590 APInt ShrMask(APInt::getLowBitsSet(BitWidth, BitWidth - OpRHSVal));
3591 Constant *C = ConstantInt::get(AndRHS->getValue() & ShrMask);
Reid Spencer2a499b02006-12-13 17:19:09 +00003592 if (C == AndRHS) { // Masking out bits shifted in.
Reid Spencer13bc5d72006-12-12 09:18:51 +00003593 // (Val ashr C1) & C2 -> (Val lshr C1) & C2
Reid Spencerfdff9382006-11-08 06:47:33 +00003594 // Make the argument unsigned.
3595 Value *ShVal = Op->getOperand(0);
Reid Spencer2341c222007-02-02 02:16:23 +00003596 ShVal = InsertNewInstBefore(
Reid Spencer0d5f9232007-02-02 14:08:20 +00003597 BinaryOperator::createLShr(ShVal, OpRHS,
Reid Spencer2341c222007-02-02 02:16:23 +00003598 Op->getName()), TheAnd);
Reid Spencer2a499b02006-12-13 17:19:09 +00003599 return BinaryOperator::createAnd(ShVal, AndRHS, TheAnd.getName());
Chris Lattner7e794272004-09-24 15:21:34 +00003600 }
Chris Lattner2da29172003-09-19 19:05:02 +00003601 }
3602 break;
Chris Lattnerba1cb382003-09-19 17:17:26 +00003603 }
3604 return 0;
3605}
3606
Chris Lattner6d14f2a2002-08-09 23:47:40 +00003607
Chris Lattner6862fbd2004-09-29 17:40:11 +00003608/// InsertRangeTest - Emit a computation of: (V >= Lo && V < Hi) if Inside is
3609/// true, otherwise (V < Lo || V >= Hi). In pratice, we emit the more efficient
Reid Spencer266e42b2006-12-23 06:05:41 +00003610/// (V-Lo) <u Hi-Lo. This method expects that Lo <= Hi. isSigned indicates
3611/// whether to treat the V, Lo and HI as signed or not. IB is the location to
Chris Lattner6862fbd2004-09-29 17:40:11 +00003612/// insert new instructions.
3613Instruction *InstCombiner::InsertRangeTest(Value *V, Constant *Lo, Constant *Hi,
Reid Spencer266e42b2006-12-23 06:05:41 +00003614 bool isSigned, bool Inside,
3615 Instruction &IB) {
Zhou Sheng75b871f2007-01-11 12:24:14 +00003616 assert(cast<ConstantInt>(ConstantExpr::getICmp((isSigned ?
Reid Spencercddc9df2007-01-12 04:24:46 +00003617 ICmpInst::ICMP_SLE:ICmpInst::ICMP_ULE), Lo, Hi))->getZExtValue() &&
Chris Lattner6862fbd2004-09-29 17:40:11 +00003618 "Lo is not <= Hi in range emission code!");
Reid Spencer266e42b2006-12-23 06:05:41 +00003619
Chris Lattner6862fbd2004-09-29 17:40:11 +00003620 if (Inside) {
3621 if (Lo == Hi) // Trivially false.
Reid Spencer266e42b2006-12-23 06:05:41 +00003622 return new ICmpInst(ICmpInst::ICMP_NE, V, V);
Misha Brukmanb1c93172005-04-21 23:48:37 +00003623
Reid Spencer266e42b2006-12-23 06:05:41 +00003624 // V >= Min && V < Hi --> V < Hi
Zhou Sheng75b871f2007-01-11 12:24:14 +00003625 if (cast<ConstantInt>(Lo)->isMinValue(isSigned)) {
Reid Spencerf4071162007-03-21 23:19:50 +00003626 ICmpInst::Predicate pred = (isSigned ?
Reid Spencer266e42b2006-12-23 06:05:41 +00003627 ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT);
3628 return new ICmpInst(pred, V, Hi);
3629 }
3630
3631 // Emit V-Lo <u Hi-Lo
3632 Constant *NegLo = ConstantExpr::getNeg(Lo);
3633 Instruction *Add = BinaryOperator::createAdd(V, NegLo, V->getName()+".off");
Chris Lattner6862fbd2004-09-29 17:40:11 +00003634 InsertNewInstBefore(Add, IB);
Reid Spencer266e42b2006-12-23 06:05:41 +00003635 Constant *UpperBound = ConstantExpr::getAdd(NegLo, Hi);
3636 return new ICmpInst(ICmpInst::ICMP_ULT, Add, UpperBound);
Chris Lattner6862fbd2004-09-29 17:40:11 +00003637 }
3638
3639 if (Lo == Hi) // Trivially true.
Reid Spencer266e42b2006-12-23 06:05:41 +00003640 return new ICmpInst(ICmpInst::ICMP_EQ, V, V);
Chris Lattner6862fbd2004-09-29 17:40:11 +00003641
Reid Spencerf4071162007-03-21 23:19:50 +00003642 // V < Min || V >= Hi -> V > Hi-1
Chris Lattner6862fbd2004-09-29 17:40:11 +00003643 Hi = SubOne(cast<ConstantInt>(Hi));
Zhou Sheng75b871f2007-01-11 12:24:14 +00003644 if (cast<ConstantInt>(Lo)->isMinValue(isSigned)) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003645 ICmpInst::Predicate pred = (isSigned ?
3646 ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT);
3647 return new ICmpInst(pred, V, Hi);
3648 }
Reid Spencere0fc4df2006-10-20 07:07:24 +00003649
Reid Spencerf4071162007-03-21 23:19:50 +00003650 // Emit V-Lo >u Hi-1-Lo
3651 // Note that Hi has already had one subtracted from it, above.
3652 ConstantInt *NegLo = cast<ConstantInt>(ConstantExpr::getNeg(Lo));
Reid Spencer266e42b2006-12-23 06:05:41 +00003653 Instruction *Add = BinaryOperator::createAdd(V, NegLo, V->getName()+".off");
Chris Lattner6862fbd2004-09-29 17:40:11 +00003654 InsertNewInstBefore(Add, IB);
Reid Spencer266e42b2006-12-23 06:05:41 +00003655 Constant *LowerBound = ConstantExpr::getAdd(NegLo, Hi);
3656 return new ICmpInst(ICmpInst::ICMP_UGT, Add, LowerBound);
Chris Lattner6862fbd2004-09-29 17:40:11 +00003657}
3658
Chris Lattnerb4b25302005-09-18 07:22:02 +00003659// isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s with
3660// any number of 0s on either side. The 1s are allowed to wrap from LSB to
3661// MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is
3662// not, since all 1s are not contiguous.
Zhou Sheng56cda952007-04-02 08:20:41 +00003663static bool isRunOfOnes(ConstantInt *Val, uint32_t &MB, uint32_t &ME) {
Zhou Sheng150f3bb2007-04-01 17:13:37 +00003664 const APInt& V = Val->getValue();
Reid Spencera962d182007-03-24 00:42:08 +00003665 uint32_t BitWidth = Val->getType()->getBitWidth();
3666 if (!APIntOps::isShiftedMask(BitWidth, V)) return false;
Chris Lattnerb4b25302005-09-18 07:22:02 +00003667
3668 // look for the first zero bit after the run of ones
Reid Spencera962d182007-03-24 00:42:08 +00003669 MB = BitWidth - ((V - 1) ^ V).countLeadingZeros();
Chris Lattnerb4b25302005-09-18 07:22:02 +00003670 // look for the first non-zero bit
Reid Spencera962d182007-03-24 00:42:08 +00003671 ME = V.getActiveBits();
Chris Lattnerb4b25302005-09-18 07:22:02 +00003672 return true;
3673}
3674
Chris Lattnerb4b25302005-09-18 07:22:02 +00003675/// FoldLogicalPlusAnd - This is part of an expression (LHS +/- RHS) & Mask,
3676/// where isSub determines whether the operator is a sub. If we can fold one of
3677/// the following xforms:
Chris Lattneraf517572005-09-18 04:24:45 +00003678///
3679/// ((A & N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == Mask
3680/// ((A | N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == 0
3681/// ((A ^ N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == 0
3682///
3683/// return (A +/- B).
3684///
3685Value *InstCombiner::FoldLogicalPlusAnd(Value *LHS, Value *RHS,
Zhou Sheng75b871f2007-01-11 12:24:14 +00003686 ConstantInt *Mask, bool isSub,
Chris Lattneraf517572005-09-18 04:24:45 +00003687 Instruction &I) {
3688 Instruction *LHSI = dyn_cast<Instruction>(LHS);
3689 if (!LHSI || LHSI->getNumOperands() != 2 ||
3690 !isa<ConstantInt>(LHSI->getOperand(1))) return 0;
3691
3692 ConstantInt *N = cast<ConstantInt>(LHSI->getOperand(1));
3693
3694 switch (LHSI->getOpcode()) {
3695 default: return 0;
3696 case Instruction::And:
Reid Spencer80263aa2007-03-25 05:33:51 +00003697 if (And(N, Mask) == Mask) {
Chris Lattnerb4b25302005-09-18 07:22:02 +00003698 // If the AndRHS is a power of two minus one (0+1+), this is simple.
Zhou Shenge9ebd3f2007-03-24 15:34:37 +00003699 if ((Mask->getValue().countLeadingZeros() +
3700 Mask->getValue().countPopulation()) ==
3701 Mask->getValue().getBitWidth())
Chris Lattnerb4b25302005-09-18 07:22:02 +00003702 break;
3703
3704 // Otherwise, if Mask is 0+1+0+, and if B is known to have the low 0+
3705 // part, we don't need any explicit masks to take them out of A. If that
3706 // is all N is, ignore it.
Zhou Sheng56cda952007-04-02 08:20:41 +00003707 uint32_t MB = 0, ME = 0;
Chris Lattnerb4b25302005-09-18 07:22:02 +00003708 if (isRunOfOnes(Mask, MB, ME)) { // begin/end bit of run, inclusive
Reid Spencer6274c722007-03-23 18:46:34 +00003709 uint32_t BitWidth = cast<IntegerType>(RHS->getType())->getBitWidth();
Zhou Shengb3a80b12007-03-29 08:15:12 +00003710 APInt Mask(APInt::getLowBitsSet(BitWidth, MB-1));
Chris Lattnerc3ebf402006-02-07 07:27:52 +00003711 if (MaskedValueIsZero(RHS, Mask))
Chris Lattnerb4b25302005-09-18 07:22:02 +00003712 break;
3713 }
3714 }
Chris Lattneraf517572005-09-18 04:24:45 +00003715 return 0;
3716 case Instruction::Or:
3717 case Instruction::Xor:
Chris Lattnerb4b25302005-09-18 07:22:02 +00003718 // If the AndRHS is a power of two minus one (0+1+), and N&Mask == 0
Zhou Shenge9ebd3f2007-03-24 15:34:37 +00003719 if ((Mask->getValue().countLeadingZeros() +
3720 Mask->getValue().countPopulation()) == Mask->getValue().getBitWidth()
Reid Spencer54d5b1b2007-03-26 23:58:26 +00003721 && And(N, Mask)->isZero())
Chris Lattneraf517572005-09-18 04:24:45 +00003722 break;
3723 return 0;
3724 }
3725
3726 Instruction *New;
3727 if (isSub)
3728 New = BinaryOperator::createSub(LHSI->getOperand(0), RHS, "fold");
3729 else
3730 New = BinaryOperator::createAdd(LHSI->getOperand(0), RHS, "fold");
3731 return InsertNewInstBefore(New, I);
3732}
3733
Chris Lattner113f4f42002-06-25 16:13:24 +00003734Instruction *InstCombiner::visitAnd(BinaryOperator &I) {
Chris Lattnerdcf240a2003-03-10 21:43:22 +00003735 bool Changed = SimplifyCommutative(I);
Chris Lattner113f4f42002-06-25 16:13:24 +00003736 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
Chris Lattnerf4cdbf32002-05-06 16:14:14 +00003737
Chris Lattner81a7a232004-10-16 18:11:37 +00003738 if (isa<UndefValue>(Op1)) // X & undef -> 0
3739 return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
3740
Chris Lattner86102b82005-01-01 16:22:27 +00003741 // and X, X = X
3742 if (Op0 == Op1)
Chris Lattnere6794492002-08-12 21:17:25 +00003743 return ReplaceInstUsesWith(I, Op1);
Chris Lattnerf4cdbf32002-05-06 16:14:14 +00003744
Chris Lattner5b2edb12006-02-12 08:02:11 +00003745 // See if we can simplify any instructions used by the instruction whose sole
Chris Lattner5997cf92006-02-08 03:25:32 +00003746 // purpose is to compute bits we don't care about.
Reid Spencerd84d35b2007-02-15 02:26:10 +00003747 if (!isa<VectorType>(I.getType())) {
Reid Spencerb722f2b2007-03-22 22:19:58 +00003748 uint32_t BitWidth = cast<IntegerType>(I.getType())->getBitWidth();
3749 APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0);
3750 if (SimplifyDemandedBits(&I, APInt::getAllOnesValue(BitWidth),
Chris Lattner120ab032007-01-18 22:16:33 +00003751 KnownZero, KnownOne))
Reid Spencer54d5b1b2007-03-26 23:58:26 +00003752 return &I;
Chris Lattner120ab032007-01-18 22:16:33 +00003753 } else {
Reid Spencerd84d35b2007-02-15 02:26:10 +00003754 if (ConstantVector *CP = dyn_cast<ConstantVector>(Op1)) {
Chris Lattnerf14e5172007-06-15 05:26:55 +00003755 if (CP->isAllOnesValue()) // X & <-1,-1> -> X
Chris Lattner120ab032007-01-18 22:16:33 +00003756 return ReplaceInstUsesWith(I, I.getOperand(0));
Chris Lattnerf14e5172007-06-15 05:26:55 +00003757 } else if (isa<ConstantAggregateZero>(Op1)) {
3758 return ReplaceInstUsesWith(I, Op1); // X & <0,0> -> <0,0>
Chris Lattner120ab032007-01-18 22:16:33 +00003759 }
3760 }
Chris Lattner5997cf92006-02-08 03:25:32 +00003761
Zhou Sheng75b871f2007-01-11 12:24:14 +00003762 if (ConstantInt *AndRHS = dyn_cast<ConstantInt>(Op1)) {
Zhou Sheng150f3bb2007-04-01 17:13:37 +00003763 const APInt& AndRHSMask = AndRHS->getValue();
3764 APInt NotAndRHS(~AndRHSMask);
Chris Lattner86102b82005-01-01 16:22:27 +00003765
Chris Lattnerba1cb382003-09-19 17:17:26 +00003766 // Optimize a variety of ((val OP C1) & C2) combinations...
Reid Spencer2341c222007-02-02 02:16:23 +00003767 if (isa<BinaryOperator>(Op0)) {
Chris Lattnerba1cb382003-09-19 17:17:26 +00003768 Instruction *Op0I = cast<Instruction>(Op0);
Chris Lattner86102b82005-01-01 16:22:27 +00003769 Value *Op0LHS = Op0I->getOperand(0);
3770 Value *Op0RHS = Op0I->getOperand(1);
3771 switch (Op0I->getOpcode()) {
3772 case Instruction::Xor:
3773 case Instruction::Or:
Chris Lattner9e2c7fa2005-01-23 20:26:55 +00003774 // If the mask is only needed on one incoming arm, push it up.
3775 if (Op0I->hasOneUse()) {
3776 if (MaskedValueIsZero(Op0LHS, NotAndRHS)) {
3777 // Not masking anything out for the LHS, move to RHS.
3778 Instruction *NewRHS = BinaryOperator::createAnd(Op0RHS, AndRHS,
3779 Op0RHS->getName()+".masked");
3780 InsertNewInstBefore(NewRHS, I);
3781 return BinaryOperator::create(
3782 cast<BinaryOperator>(Op0I)->getOpcode(), Op0LHS, NewRHS);
Misha Brukmanb1c93172005-04-21 23:48:37 +00003783 }
Chris Lattnerc3ebf402006-02-07 07:27:52 +00003784 if (!isa<Constant>(Op0RHS) &&
Chris Lattner9e2c7fa2005-01-23 20:26:55 +00003785 MaskedValueIsZero(Op0RHS, NotAndRHS)) {
3786 // Not masking anything out for the RHS, move to LHS.
3787 Instruction *NewLHS = BinaryOperator::createAnd(Op0LHS, AndRHS,
3788 Op0LHS->getName()+".masked");
3789 InsertNewInstBefore(NewLHS, I);
3790 return BinaryOperator::create(
3791 cast<BinaryOperator>(Op0I)->getOpcode(), NewLHS, Op0RHS);
3792 }
3793 }
3794
Chris Lattner86102b82005-01-01 16:22:27 +00003795 break;
Chris Lattneraf517572005-09-18 04:24:45 +00003796 case Instruction::Add:
Chris Lattnerb4b25302005-09-18 07:22:02 +00003797 // ((A & N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == AndRHS.
3798 // ((A | N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == 0
3799 // ((A ^ N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == 0
3800 if (Value *V = FoldLogicalPlusAnd(Op0LHS, Op0RHS, AndRHS, false, I))
3801 return BinaryOperator::createAnd(V, AndRHS);
3802 if (Value *V = FoldLogicalPlusAnd(Op0RHS, Op0LHS, AndRHS, false, I))
3803 return BinaryOperator::createAnd(V, AndRHS); // Add commutes
Chris Lattneraf517572005-09-18 04:24:45 +00003804 break;
3805
3806 case Instruction::Sub:
Chris Lattnerb4b25302005-09-18 07:22:02 +00003807 // ((A & N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == AndRHS.
3808 // ((A | N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == 0
3809 // ((A ^ N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == 0
3810 if (Value *V = FoldLogicalPlusAnd(Op0LHS, Op0RHS, AndRHS, true, I))
3811 return BinaryOperator::createAnd(V, AndRHS);
Chris Lattneraf517572005-09-18 04:24:45 +00003812 break;
Chris Lattner86102b82005-01-01 16:22:27 +00003813 }
3814
Chris Lattner16464b32003-07-23 19:25:52 +00003815 if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1)))
Chris Lattner86102b82005-01-01 16:22:27 +00003816 if (Instruction *Res = OptAndOp(Op0I, Op0CI, AndRHS, I))
Chris Lattnerba1cb382003-09-19 17:17:26 +00003817 return Res;
Chris Lattner86102b82005-01-01 16:22:27 +00003818 } else if (CastInst *CI = dyn_cast<CastInst>(Op0)) {
Chris Lattner2c14cf72005-08-07 07:03:10 +00003819 // If this is an integer truncation or change from signed-to-unsigned, and
3820 // if the source is an and/or with immediate, transform it. This
3821 // frequently occurs for bitfield accesses.
3822 if (Instruction *CastOp = dyn_cast<Instruction>(CI->getOperand(0))) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003823 if ((isa<TruncInst>(CI) || isa<BitCastInst>(CI)) &&
Chris Lattner2c14cf72005-08-07 07:03:10 +00003824 CastOp->getNumOperands() == 2)
Anton Korobeynikov1bfd1212008-02-20 11:26:25 +00003825 if (ConstantInt *AndCI = dyn_cast<ConstantInt>(CastOp->getOperand(1))) {
Chris Lattner2c14cf72005-08-07 07:03:10 +00003826 if (CastOp->getOpcode() == Instruction::And) {
3827 // Change: and (cast (and X, C1) to T), C2
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003828 // into : and (cast X to T), trunc_or_bitcast(C1)&C2
3829 // This will fold the two constants together, which may allow
3830 // other simplifications.
Reid Spencerbb65ebf2006-12-12 23:36:14 +00003831 Instruction *NewCast = CastInst::createTruncOrBitCast(
3832 CastOp->getOperand(0), I.getType(),
3833 CastOp->getName()+".shrunk");
Chris Lattner2c14cf72005-08-07 07:03:10 +00003834 NewCast = InsertNewInstBefore(NewCast, I);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003835 // trunc_or_bitcast(C1)&C2
Reid Spencerbb65ebf2006-12-12 23:36:14 +00003836 Constant *C3 = ConstantExpr::getTruncOrBitCast(AndCI,I.getType());
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003837 C3 = ConstantExpr::getAnd(C3, AndRHS);
Chris Lattner2c14cf72005-08-07 07:03:10 +00003838 return BinaryOperator::createAnd(NewCast, C3);
3839 } else if (CastOp->getOpcode() == Instruction::Or) {
3840 // Change: and (cast (or X, C1) to T), C2
3841 // into : trunc(C1)&C2 iff trunc(C1)&C2 == C2
Chris Lattner2dc148e2006-12-12 19:11:20 +00003842 Constant *C3 = ConstantExpr::getTruncOrBitCast(AndCI,I.getType());
Chris Lattner2c14cf72005-08-07 07:03:10 +00003843 if (ConstantExpr::getAnd(C3, AndRHS) == AndRHS) // trunc(C1)&C2
3844 return ReplaceInstUsesWith(I, AndRHS);
3845 }
Anton Korobeynikov1bfd1212008-02-20 11:26:25 +00003846 }
Chris Lattner2c14cf72005-08-07 07:03:10 +00003847 }
Chris Lattner33217db2003-07-23 19:36:21 +00003848 }
Chris Lattner183b3362004-04-09 19:05:30 +00003849
3850 // Try to fold constant and into select arguments.
3851 if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
Chris Lattner86102b82005-01-01 16:22:27 +00003852 if (Instruction *R = FoldOpIntoSelect(I, SI, this))
Chris Lattner183b3362004-04-09 19:05:30 +00003853 return R;
Chris Lattner6a4adcd2004-09-29 05:07:12 +00003854 if (isa<PHINode>(Op0))
3855 if (Instruction *NV = FoldOpIntoPhi(I))
3856 return NV;
Chris Lattner49b47ae2003-07-23 17:57:01 +00003857 }
3858
Chris Lattnerbb74e222003-03-10 23:06:50 +00003859 Value *Op0NotVal = dyn_castNotVal(Op0);
3860 Value *Op1NotVal = dyn_castNotVal(Op1);
Chris Lattner3082c5a2003-02-18 19:28:33 +00003861
Chris Lattner023a4832004-06-18 06:07:51 +00003862 if (Op0NotVal == Op1 || Op1NotVal == Op0) // A & ~A == ~A & A == 0
3863 return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
3864
Misha Brukman9c003d82004-07-30 12:50:08 +00003865 // (~A & ~B) == (~(A | B)) - De Morgan's Law
Chris Lattnerbb74e222003-03-10 23:06:50 +00003866 if (Op0NotVal && Op1NotVal && isOnlyUse(Op0) && isOnlyUse(Op1)) {
Chris Lattnerdf20a4d2004-06-10 02:07:29 +00003867 Instruction *Or = BinaryOperator::createOr(Op0NotVal, Op1NotVal,
3868 I.getName()+".demorgan");
Chris Lattner49b47ae2003-07-23 17:57:01 +00003869 InsertNewInstBefore(Or, I);
Chris Lattner3082c5a2003-02-18 19:28:33 +00003870 return BinaryOperator::createNot(Or);
3871 }
Chris Lattner8b10ab32006-02-13 23:07:23 +00003872
3873 {
Chris Lattner481e28b2007-06-15 05:58:24 +00003874 Value *A = 0, *B = 0, *C = 0, *D = 0;
3875 if (match(Op0, m_Or(m_Value(A), m_Value(B)))) {
Chris Lattner8b10ab32006-02-13 23:07:23 +00003876 if (A == Op1 || B == Op1) // (A | ?) & A --> A
3877 return ReplaceInstUsesWith(I, Op1);
Chris Lattner481e28b2007-06-15 05:58:24 +00003878
3879 // (A|B) & ~(A&B) -> A^B
3880 if (match(Op1, m_Not(m_And(m_Value(C), m_Value(D))))) {
3881 if ((A == C && B == D) || (A == D && B == C))
3882 return BinaryOperator::createXor(A, B);
3883 }
3884 }
3885
3886 if (match(Op1, m_Or(m_Value(A), m_Value(B)))) {
Chris Lattner8b10ab32006-02-13 23:07:23 +00003887 if (A == Op0 || B == Op0) // A & (A | ?) --> A
3888 return ReplaceInstUsesWith(I, Op0);
Chris Lattner481e28b2007-06-15 05:58:24 +00003889
3890 // ~(A&B) & (A|B) -> A^B
3891 if (match(Op0, m_Not(m_And(m_Value(C), m_Value(D))))) {
3892 if ((A == C && B == D) || (A == D && B == C))
3893 return BinaryOperator::createXor(A, B);
3894 }
3895 }
Chris Lattnerdcd07922006-04-01 08:03:55 +00003896
3897 if (Op0->hasOneUse() &&
3898 match(Op0, m_Xor(m_Value(A), m_Value(B)))) {
3899 if (A == Op1) { // (A^B)&A -> A&(A^B)
3900 I.swapOperands(); // Simplify below
3901 std::swap(Op0, Op1);
3902 } else if (B == Op1) { // (A^B)&B -> B&(B^A)
3903 cast<BinaryOperator>(Op0)->swapOperands();
3904 I.swapOperands(); // Simplify below
3905 std::swap(Op0, Op1);
3906 }
3907 }
3908 if (Op1->hasOneUse() &&
3909 match(Op1, m_Xor(m_Value(A), m_Value(B)))) {
3910 if (B == Op0) { // B&(A^B) -> B&(B^A)
3911 cast<BinaryOperator>(Op1)->swapOperands();
3912 std::swap(A, B);
3913 }
3914 if (A == Op0) { // A&(A^B) -> A & ~B
3915 Instruction *NotB = BinaryOperator::createNot(B, "tmp");
3916 InsertNewInstBefore(NotB, I);
3917 return BinaryOperator::createAnd(A, NotB);
3918 }
3919 }
Chris Lattner8b10ab32006-02-13 23:07:23 +00003920 }
3921
Reid Spencer266e42b2006-12-23 06:05:41 +00003922 if (ICmpInst *RHS = dyn_cast<ICmpInst>(Op1)) {
3923 // (icmp1 A, B) & (icmp2 A, B) --> (icmp3 A, B)
3924 if (Instruction *R = AssociativeOpt(I, FoldICmpLogical(*this, RHS)))
Chris Lattner3ac7c262003-08-13 20:16:26 +00003925 return R;
3926
Chris Lattner623826c2004-09-28 21:48:02 +00003927 Value *LHSVal, *RHSVal;
3928 ConstantInt *LHSCst, *RHSCst;
Reid Spencer266e42b2006-12-23 06:05:41 +00003929 ICmpInst::Predicate LHSCC, RHSCC;
3930 if (match(Op0, m_ICmp(LHSCC, m_Value(LHSVal), m_ConstantInt(LHSCst))))
3931 if (match(RHS, m_ICmp(RHSCC, m_Value(RHSVal), m_ConstantInt(RHSCst))))
3932 if (LHSVal == RHSVal && // Found (X icmp C1) & (X icmp C2)
3933 // ICMP_[GL]E X, CST is folded to ICMP_[GL]T elsewhere.
3934 LHSCC != ICmpInst::ICMP_UGE && LHSCC != ICmpInst::ICMP_ULE &&
3935 RHSCC != ICmpInst::ICMP_UGE && RHSCC != ICmpInst::ICMP_ULE &&
3936 LHSCC != ICmpInst::ICMP_SGE && LHSCC != ICmpInst::ICMP_SLE &&
Chris Lattner1985d962007-11-22 23:47:13 +00003937 RHSCC != ICmpInst::ICMP_SGE && RHSCC != ICmpInst::ICMP_SLE &&
3938
3939 // Don't try to fold ICMP_SLT + ICMP_ULT.
3940 (ICmpInst::isEquality(LHSCC) || ICmpInst::isEquality(RHSCC) ||
3941 ICmpInst::isSignedPredicate(LHSCC) ==
3942 ICmpInst::isSignedPredicate(RHSCC))) {
Chris Lattner623826c2004-09-28 21:48:02 +00003943 // Ensure that the larger constant is on the RHS.
Chris Lattner5bc253c2008-01-13 20:59:02 +00003944 ICmpInst::Predicate GT;
3945 if (ICmpInst::isSignedPredicate(LHSCC) ||
3946 (ICmpInst::isEquality(LHSCC) &&
3947 ICmpInst::isSignedPredicate(RHSCC)))
3948 GT = ICmpInst::ICMP_SGT;
3949 else
3950 GT = ICmpInst::ICMP_UGT;
3951
Reid Spencer266e42b2006-12-23 06:05:41 +00003952 Constant *Cmp = ConstantExpr::getICmp(GT, LHSCst, RHSCst);
3953 ICmpInst *LHS = cast<ICmpInst>(Op0);
Reid Spencercddc9df2007-01-12 04:24:46 +00003954 if (cast<ConstantInt>(Cmp)->getZExtValue()) {
Chris Lattner623826c2004-09-28 21:48:02 +00003955 std::swap(LHS, RHS);
3956 std::swap(LHSCst, RHSCst);
3957 std::swap(LHSCC, RHSCC);
3958 }
3959
Reid Spencer266e42b2006-12-23 06:05:41 +00003960 // At this point, we know we have have two icmp instructions
Chris Lattner623826c2004-09-28 21:48:02 +00003961 // comparing a value against two constants and and'ing the result
3962 // together. Because of the above check, we know that we only have
Reid Spencer266e42b2006-12-23 06:05:41 +00003963 // icmp eq, icmp ne, icmp [su]lt, and icmp [SU]gt here. We also know
3964 // (from the FoldICmpLogical check above), that the two constants
3965 // are not equal and that the larger constant is on the RHS
Chris Lattner623826c2004-09-28 21:48:02 +00003966 assert(LHSCst != RHSCst && "Compares not folded above?");
3967
3968 switch (LHSCC) {
3969 default: assert(0 && "Unknown integer condition code!");
Reid Spencer266e42b2006-12-23 06:05:41 +00003970 case ICmpInst::ICMP_EQ:
Chris Lattner623826c2004-09-28 21:48:02 +00003971 switch (RHSCC) {
3972 default: assert(0 && "Unknown integer condition code!");
Reid Spencer266e42b2006-12-23 06:05:41 +00003973 case ICmpInst::ICMP_EQ: // (X == 13 & X == 15) -> false
3974 case ICmpInst::ICMP_UGT: // (X == 13 & X > 15) -> false
3975 case ICmpInst::ICMP_SGT: // (X == 13 & X > 15) -> false
Zhou Sheng75b871f2007-01-11 12:24:14 +00003976 return ReplaceInstUsesWith(I, ConstantInt::getFalse());
Reid Spencer266e42b2006-12-23 06:05:41 +00003977 case ICmpInst::ICMP_NE: // (X == 13 & X != 15) -> X == 13
3978 case ICmpInst::ICMP_ULT: // (X == 13 & X < 15) -> X == 13
3979 case ICmpInst::ICMP_SLT: // (X == 13 & X < 15) -> X == 13
Chris Lattner623826c2004-09-28 21:48:02 +00003980 return ReplaceInstUsesWith(I, LHS);
3981 }
Reid Spencer266e42b2006-12-23 06:05:41 +00003982 case ICmpInst::ICMP_NE:
Chris Lattner623826c2004-09-28 21:48:02 +00003983 switch (RHSCC) {
3984 default: assert(0 && "Unknown integer condition code!");
Reid Spencer266e42b2006-12-23 06:05:41 +00003985 case ICmpInst::ICMP_ULT:
3986 if (LHSCst == SubOne(RHSCst)) // (X != 13 & X u< 14) -> X < 13
3987 return new ICmpInst(ICmpInst::ICMP_ULT, LHSVal, LHSCst);
3988 break; // (X != 13 & X u< 15) -> no change
3989 case ICmpInst::ICMP_SLT:
3990 if (LHSCst == SubOne(RHSCst)) // (X != 13 & X s< 14) -> X < 13
3991 return new ICmpInst(ICmpInst::ICMP_SLT, LHSVal, LHSCst);
3992 break; // (X != 13 & X s< 15) -> no change
3993 case ICmpInst::ICMP_EQ: // (X != 13 & X == 15) -> X == 15
3994 case ICmpInst::ICMP_UGT: // (X != 13 & X u> 15) -> X u> 15
3995 case ICmpInst::ICMP_SGT: // (X != 13 & X s> 15) -> X s> 15
Chris Lattner623826c2004-09-28 21:48:02 +00003996 return ReplaceInstUsesWith(I, RHS);
Reid Spencer266e42b2006-12-23 06:05:41 +00003997 case ICmpInst::ICMP_NE:
3998 if (LHSCst == SubOne(RHSCst)){// (X != 13 & X != 14) -> X-13 >u 1
Chris Lattner623826c2004-09-28 21:48:02 +00003999 Constant *AddCST = ConstantExpr::getNeg(LHSCst);
4000 Instruction *Add = BinaryOperator::createAdd(LHSVal, AddCST,
4001 LHSVal->getName()+".off");
4002 InsertNewInstBefore(Add, I);
Chris Lattnerc8fb6de2007-01-27 23:08:34 +00004003 return new ICmpInst(ICmpInst::ICMP_UGT, Add,
4004 ConstantInt::get(Add->getType(), 1));
Chris Lattner623826c2004-09-28 21:48:02 +00004005 }
4006 break; // (X != 13 & X != 15) -> no change
4007 }
4008 break;
Reid Spencer266e42b2006-12-23 06:05:41 +00004009 case ICmpInst::ICMP_ULT:
Chris Lattner623826c2004-09-28 21:48:02 +00004010 switch (RHSCC) {
4011 default: assert(0 && "Unknown integer condition code!");
Reid Spencer266e42b2006-12-23 06:05:41 +00004012 case ICmpInst::ICMP_EQ: // (X u< 13 & X == 15) -> false
4013 case ICmpInst::ICMP_UGT: // (X u< 13 & X u> 15) -> false
Zhou Sheng75b871f2007-01-11 12:24:14 +00004014 return ReplaceInstUsesWith(I, ConstantInt::getFalse());
Reid Spencer266e42b2006-12-23 06:05:41 +00004015 case ICmpInst::ICMP_SGT: // (X u< 13 & X s> 15) -> no change
4016 break;
4017 case ICmpInst::ICMP_NE: // (X u< 13 & X != 15) -> X u< 13
4018 case ICmpInst::ICMP_ULT: // (X u< 13 & X u< 15) -> X u< 13
Chris Lattner623826c2004-09-28 21:48:02 +00004019 return ReplaceInstUsesWith(I, LHS);
Reid Spencer266e42b2006-12-23 06:05:41 +00004020 case ICmpInst::ICMP_SLT: // (X u< 13 & X s< 15) -> no change
4021 break;
Chris Lattner623826c2004-09-28 21:48:02 +00004022 }
Reid Spencer266e42b2006-12-23 06:05:41 +00004023 break;
4024 case ICmpInst::ICMP_SLT:
Chris Lattner623826c2004-09-28 21:48:02 +00004025 switch (RHSCC) {
4026 default: assert(0 && "Unknown integer condition code!");
Reid Spencer266e42b2006-12-23 06:05:41 +00004027 case ICmpInst::ICMP_EQ: // (X s< 13 & X == 15) -> false
4028 case ICmpInst::ICMP_SGT: // (X s< 13 & X s> 15) -> false
Zhou Sheng75b871f2007-01-11 12:24:14 +00004029 return ReplaceInstUsesWith(I, ConstantInt::getFalse());
Reid Spencer266e42b2006-12-23 06:05:41 +00004030 case ICmpInst::ICMP_UGT: // (X s< 13 & X u> 15) -> no change
4031 break;
4032 case ICmpInst::ICMP_NE: // (X s< 13 & X != 15) -> X < 13
4033 case ICmpInst::ICMP_SLT: // (X s< 13 & X s< 15) -> X < 13
Chris Lattner623826c2004-09-28 21:48:02 +00004034 return ReplaceInstUsesWith(I, LHS);
Reid Spencer266e42b2006-12-23 06:05:41 +00004035 case ICmpInst::ICMP_ULT: // (X s< 13 & X u< 15) -> no change
4036 break;
Chris Lattner623826c2004-09-28 21:48:02 +00004037 }
Reid Spencer266e42b2006-12-23 06:05:41 +00004038 break;
4039 case ICmpInst::ICMP_UGT:
4040 switch (RHSCC) {
4041 default: assert(0 && "Unknown integer condition code!");
4042 case ICmpInst::ICMP_EQ: // (X u> 13 & X == 15) -> X > 13
4043 return ReplaceInstUsesWith(I, LHS);
4044 case ICmpInst::ICMP_UGT: // (X u> 13 & X u> 15) -> X u> 15
4045 return ReplaceInstUsesWith(I, RHS);
4046 case ICmpInst::ICMP_SGT: // (X u> 13 & X s> 15) -> no change
4047 break;
4048 case ICmpInst::ICMP_NE:
4049 if (RHSCst == AddOne(LHSCst)) // (X u> 13 & X != 14) -> X u> 14
4050 return new ICmpInst(LHSCC, LHSVal, RHSCst);
4051 break; // (X u> 13 & X != 15) -> no change
4052 case ICmpInst::ICMP_ULT: // (X u> 13 & X u< 15) ->(X-14) <u 1
4053 return InsertRangeTest(LHSVal, AddOne(LHSCst), RHSCst, false,
4054 true, I);
4055 case ICmpInst::ICMP_SLT: // (X u> 13 & X s< 15) -> no change
4056 break;
4057 }
4058 break;
4059 case ICmpInst::ICMP_SGT:
4060 switch (RHSCC) {
4061 default: assert(0 && "Unknown integer condition code!");
Chris Lattnerc53b1832007-11-16 06:04:17 +00004062 case ICmpInst::ICMP_EQ: // (X s> 13 & X == 15) -> X == 15
Reid Spencer266e42b2006-12-23 06:05:41 +00004063 case ICmpInst::ICMP_SGT: // (X s> 13 & X s> 15) -> X s> 15
4064 return ReplaceInstUsesWith(I, RHS);
4065 case ICmpInst::ICMP_UGT: // (X s> 13 & X u> 15) -> no change
4066 break;
4067 case ICmpInst::ICMP_NE:
4068 if (RHSCst == AddOne(LHSCst)) // (X s> 13 & X != 14) -> X s> 14
4069 return new ICmpInst(LHSCC, LHSVal, RHSCst);
4070 break; // (X s> 13 & X != 15) -> no change
4071 case ICmpInst::ICMP_SLT: // (X s> 13 & X s< 15) ->(X-14) s< 1
4072 return InsertRangeTest(LHSVal, AddOne(LHSCst), RHSCst, true,
4073 true, I);
4074 case ICmpInst::ICMP_ULT: // (X s> 13 & X u< 15) -> no change
4075 break;
4076 }
4077 break;
Chris Lattner623826c2004-09-28 21:48:02 +00004078 }
4079 }
4080 }
4081
Chris Lattner3af10532006-05-05 06:39:07 +00004082 // fold (and (cast A), (cast B)) -> (cast (and A, B))
Reid Spencer799b5bf2006-12-13 08:27:15 +00004083 if (CastInst *Op0C = dyn_cast<CastInst>(Op0))
4084 if (CastInst *Op1C = dyn_cast<CastInst>(Op1))
4085 if (Op0C->getOpcode() == Op1C->getOpcode()) { // same cast kind ?
4086 const Type *SrcTy = Op0C->getOperand(0)->getType();
Chris Lattner03c49532007-01-15 02:27:26 +00004087 if (SrcTy == Op1C->getOperand(0)->getType() && SrcTy->isInteger() &&
Reid Spencer799b5bf2006-12-13 08:27:15 +00004088 // Only do this if the casts both really cause code to be generated.
Reid Spencer266e42b2006-12-23 06:05:41 +00004089 ValueRequiresCast(Op0C->getOpcode(), Op0C->getOperand(0),
4090 I.getType(), TD) &&
4091 ValueRequiresCast(Op1C->getOpcode(), Op1C->getOperand(0),
4092 I.getType(), TD)) {
Reid Spencer799b5bf2006-12-13 08:27:15 +00004093 Instruction *NewOp = BinaryOperator::createAnd(Op0C->getOperand(0),
4094 Op1C->getOperand(0),
4095 I.getName());
4096 InsertNewInstBefore(NewOp, I);
4097 return CastInst::create(Op0C->getOpcode(), NewOp, I.getType());
4098 }
Chris Lattner3af10532006-05-05 06:39:07 +00004099 }
Chris Lattnerf05d69a2006-11-14 07:46:50 +00004100
4101 // (X >> Z) & (Y >> Z) -> (X&Y) >> Z for all shifts.
Reid Spencer2341c222007-02-02 02:16:23 +00004102 if (BinaryOperator *SI1 = dyn_cast<BinaryOperator>(Op1)) {
4103 if (BinaryOperator *SI0 = dyn_cast<BinaryOperator>(Op0))
4104 if (SI0->isShift() && SI0->getOpcode() == SI1->getOpcode() &&
Chris Lattnerf05d69a2006-11-14 07:46:50 +00004105 SI0->getOperand(1) == SI1->getOperand(1) &&
4106 (SI0->hasOneUse() || SI1->hasOneUse())) {
4107 Instruction *NewOp =
4108 InsertNewInstBefore(BinaryOperator::createAnd(SI0->getOperand(0),
4109 SI1->getOperand(0),
4110 SI0->getName()), I);
Reid Spencer2341c222007-02-02 02:16:23 +00004111 return BinaryOperator::create(SI1->getOpcode(), NewOp,
4112 SI1->getOperand(1));
Chris Lattnerf05d69a2006-11-14 07:46:50 +00004113 }
Chris Lattner3af10532006-05-05 06:39:07 +00004114 }
4115
Chris Lattnerc62877e2007-10-24 05:38:08 +00004116 // (fcmp ord x, c) & (fcmp ord y, c) -> (fcmp ord x, y)
4117 if (FCmpInst *LHS = dyn_cast<FCmpInst>(I.getOperand(0))) {
4118 if (FCmpInst *RHS = dyn_cast<FCmpInst>(I.getOperand(1))) {
4119 if (LHS->getPredicate() == FCmpInst::FCMP_ORD &&
4120 RHS->getPredicate() == FCmpInst::FCMP_ORD)
4121 if (ConstantFP *LHSC = dyn_cast<ConstantFP>(LHS->getOperand(1)))
4122 if (ConstantFP *RHSC = dyn_cast<ConstantFP>(RHS->getOperand(1))) {
4123 // If either of the constants are nans, then the whole thing returns
4124 // false.
Chris Lattner55b83022007-10-24 18:54:45 +00004125 if (LHSC->getValueAPF().isNaN() || RHSC->getValueAPF().isNaN())
Chris Lattnerc62877e2007-10-24 05:38:08 +00004126 return ReplaceInstUsesWith(I, ConstantInt::getFalse());
4127 return new FCmpInst(FCmpInst::FCMP_ORD, LHS->getOperand(0),
4128 RHS->getOperand(0));
4129 }
4130 }
4131 }
4132
Chris Lattner113f4f42002-06-25 16:13:24 +00004133 return Changed ? &I : 0;
Chris Lattnerf4cdbf32002-05-06 16:14:14 +00004134}
4135
Chris Lattnerc482a9e2006-06-15 19:07:26 +00004136/// CollectBSwapParts - Look to see if the specified value defines a single byte
4137/// in the result. If it does, and if the specified byte hasn't been filled in
4138/// yet, fill it in and return false.
Chris Lattner99c6cf62007-02-15 22:52:10 +00004139static bool CollectBSwapParts(Value *V, SmallVector<Value*, 8> &ByteValues) {
Chris Lattnerc482a9e2006-06-15 19:07:26 +00004140 Instruction *I = dyn_cast<Instruction>(V);
4141 if (I == 0) return true;
4142
4143 // If this is an or instruction, it is an inner node of the bswap.
4144 if (I->getOpcode() == Instruction::Or)
4145 return CollectBSwapParts(I->getOperand(0), ByteValues) ||
4146 CollectBSwapParts(I->getOperand(1), ByteValues);
4147
Zhou Shengb25806f2007-03-30 09:29:48 +00004148 uint32_t BitWidth = I->getType()->getPrimitiveSizeInBits();
Chris Lattnerc482a9e2006-06-15 19:07:26 +00004149 // If this is a shift by a constant int, and it is "24", then its operand
4150 // defines a byte. We only handle unsigned types here.
Reid Spencer2341c222007-02-02 02:16:23 +00004151 if (I->isShift() && isa<ConstantInt>(I->getOperand(1))) {
Chris Lattnerc482a9e2006-06-15 19:07:26 +00004152 // Not shifting the entire input by N-1 bytes?
Zhou Shengb25806f2007-03-30 09:29:48 +00004153 if (cast<ConstantInt>(I->getOperand(1))->getLimitedValue(BitWidth) !=
Chris Lattnerc482a9e2006-06-15 19:07:26 +00004154 8*(ByteValues.size()-1))
4155 return true;
4156
4157 unsigned DestNo;
4158 if (I->getOpcode() == Instruction::Shl) {
4159 // X << 24 defines the top byte with the lowest of the input bytes.
4160 DestNo = ByteValues.size()-1;
4161 } else {
4162 // X >>u 24 defines the low byte with the highest of the input bytes.
4163 DestNo = 0;
4164 }
4165
4166 // If the destination byte value is already defined, the values are or'd
4167 // together, which isn't a bswap (unless it's an or of the same bits).
4168 if (ByteValues[DestNo] && ByteValues[DestNo] != I->getOperand(0))
4169 return true;
4170 ByteValues[DestNo] = I->getOperand(0);
4171 return false;
4172 }
4173
4174 // Otherwise, we can only handle and(shift X, imm), imm). Bail out of if we
4175 // don't have this.
4176 Value *Shift = 0, *ShiftLHS = 0;
4177 ConstantInt *AndAmt = 0, *ShiftAmt = 0;
4178 if (!match(I, m_And(m_Value(Shift), m_ConstantInt(AndAmt))) ||
4179 !match(Shift, m_Shift(m_Value(ShiftLHS), m_ConstantInt(ShiftAmt))))
4180 return true;
4181 Instruction *SI = cast<Instruction>(Shift);
4182
4183 // Make sure that the shift amount is by a multiple of 8 and isn't too big.
Zhou Shengb25806f2007-03-30 09:29:48 +00004184 if (ShiftAmt->getLimitedValue(BitWidth) & 7 ||
4185 ShiftAmt->getLimitedValue(BitWidth) > 8*ByteValues.size())
Chris Lattnerc482a9e2006-06-15 19:07:26 +00004186 return true;
4187
4188 // Turn 0xFF -> 0, 0xFF00 -> 1, 0xFF0000 -> 2, etc.
4189 unsigned DestByte;
Zhou Shengb25806f2007-03-30 09:29:48 +00004190 if (AndAmt->getValue().getActiveBits() > 64)
4191 return true;
4192 uint64_t AndAmtVal = AndAmt->getZExtValue();
Chris Lattnerc482a9e2006-06-15 19:07:26 +00004193 for (DestByte = 0; DestByte != ByteValues.size(); ++DestByte)
Zhou Shengb25806f2007-03-30 09:29:48 +00004194 if (AndAmtVal == uint64_t(0xFF) << 8*DestByte)
Chris Lattnerc482a9e2006-06-15 19:07:26 +00004195 break;
4196 // Unknown mask for bswap.
4197 if (DestByte == ByteValues.size()) return true;
4198
Reid Spencere0fc4df2006-10-20 07:07:24 +00004199 unsigned ShiftBytes = ShiftAmt->getZExtValue()/8;
Chris Lattnerc482a9e2006-06-15 19:07:26 +00004200 unsigned SrcByte;
4201 if (SI->getOpcode() == Instruction::Shl)
4202 SrcByte = DestByte - ShiftBytes;
4203 else
4204 SrcByte = DestByte + ShiftBytes;
4205
4206 // If the SrcByte isn't a bswapped value from the DestByte, reject it.
4207 if (SrcByte != ByteValues.size()-DestByte-1)
4208 return true;
4209
4210 // If the destination byte value is already defined, the values are or'd
4211 // together, which isn't a bswap (unless it's an or of the same bits).
4212 if (ByteValues[DestByte] && ByteValues[DestByte] != SI->getOperand(0))
4213 return true;
4214 ByteValues[DestByte] = SI->getOperand(0);
4215 return false;
4216}
4217
4218/// MatchBSwap - Given an OR instruction, check to see if this is a bswap idiom.
4219/// If so, insert the new bswap intrinsic and return it.
4220Instruction *InstCombiner::MatchBSwap(BinaryOperator &I) {
Chris Lattnerc3eeb422007-04-01 20:57:36 +00004221 const IntegerType *ITy = dyn_cast<IntegerType>(I.getType());
4222 if (!ITy || ITy->getBitWidth() % 16)
4223 return 0; // Can only bswap pairs of bytes. Can't do vectors.
Chris Lattnerc482a9e2006-06-15 19:07:26 +00004224
4225 /// ByteValues - For each byte of the result, we keep track of which value
4226 /// defines each byte.
Chris Lattner99c6cf62007-02-15 22:52:10 +00004227 SmallVector<Value*, 8> ByteValues;
Chris Lattnerc3eeb422007-04-01 20:57:36 +00004228 ByteValues.resize(ITy->getBitWidth()/8);
Chris Lattnerc482a9e2006-06-15 19:07:26 +00004229
4230 // Try to find all the pieces corresponding to the bswap.
4231 if (CollectBSwapParts(I.getOperand(0), ByteValues) ||
4232 CollectBSwapParts(I.getOperand(1), ByteValues))
4233 return 0;
4234
4235 // Check to see if all of the bytes come from the same value.
4236 Value *V = ByteValues[0];
4237 if (V == 0) return 0; // Didn't find a byte? Must be zero.
4238
4239 // Check to make sure that all of the bytes come from the same value.
4240 for (unsigned i = 1, e = ByteValues.size(); i != e; ++i)
4241 if (ByteValues[i] != V)
4242 return 0;
Chandler Carruth7132e002007-08-04 01:51:18 +00004243 const Type *Tys[] = { ITy };
Chris Lattnerc482a9e2006-06-15 19:07:26 +00004244 Module *M = I.getParent()->getParent()->getParent();
Chandler Carruth7132e002007-08-04 01:51:18 +00004245 Function *F = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 1);
Gabor Greife9ecc682008-04-06 20:25:17 +00004246 return CallInst::Create(F, V);
Chris Lattnerc482a9e2006-06-15 19:07:26 +00004247}
4248
4249
Chris Lattner113f4f42002-06-25 16:13:24 +00004250Instruction *InstCombiner::visitOr(BinaryOperator &I) {
Chris Lattnerdcf240a2003-03-10 21:43:22 +00004251 bool Changed = SimplifyCommutative(I);
Chris Lattner113f4f42002-06-25 16:13:24 +00004252 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
Chris Lattnerf4cdbf32002-05-06 16:14:14 +00004253
Chris Lattner3a8248f2007-03-24 23:56:43 +00004254 if (isa<UndefValue>(Op1)) // X | undef -> -1
Chris Lattner37338922007-06-15 06:23:19 +00004255 return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType()));
Chris Lattner81a7a232004-10-16 18:11:37 +00004256
Chris Lattner5b2edb12006-02-12 08:02:11 +00004257 // or X, X = X
4258 if (Op0 == Op1)
Chris Lattnere6794492002-08-12 21:17:25 +00004259 return ReplaceInstUsesWith(I, Op0);
Chris Lattnerf4cdbf32002-05-06 16:14:14 +00004260
Chris Lattner5b2edb12006-02-12 08:02:11 +00004261 // See if we can simplify any instructions used by the instruction whose sole
4262 // purpose is to compute bits we don't care about.
Chris Lattner3a8248f2007-03-24 23:56:43 +00004263 if (!isa<VectorType>(I.getType())) {
4264 uint32_t BitWidth = cast<IntegerType>(I.getType())->getBitWidth();
4265 APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0);
4266 if (SimplifyDemandedBits(&I, APInt::getAllOnesValue(BitWidth),
4267 KnownZero, KnownOne))
4268 return &I;
Chris Lattnerf14e5172007-06-15 05:26:55 +00004269 } else if (isa<ConstantAggregateZero>(Op1)) {
4270 return ReplaceInstUsesWith(I, Op0); // X | <0,0> -> X
4271 } else if (ConstantVector *CP = dyn_cast<ConstantVector>(Op1)) {
4272 if (CP->isAllOnesValue()) // X | <-1,-1> -> <-1,-1>
4273 return ReplaceInstUsesWith(I, I.getOperand(1));
Chris Lattner3a8248f2007-03-24 23:56:43 +00004274 }
Chris Lattnerf14e5172007-06-15 05:26:55 +00004275
4276
Chris Lattner5b2edb12006-02-12 08:02:11 +00004277
Chris Lattnerf4cdbf32002-05-06 16:14:14 +00004278 // or X, -1 == -1
Zhou Sheng75b871f2007-01-11 12:24:14 +00004279 if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) {
Chris Lattner330628a2006-01-06 17:59:59 +00004280 ConstantInt *C1 = 0; Value *X = 0;
Chris Lattnerd4252a72004-07-30 07:50:03 +00004281 // (X & C1) | C2 --> (X | C2) & (C1|C2)
4282 if (match(Op0, m_And(m_Value(X), m_ConstantInt(C1))) && isOnlyUse(Op0)) {
Chris Lattner6e0123b2007-02-11 01:23:03 +00004283 Instruction *Or = BinaryOperator::createOr(X, RHS);
Chris Lattnerd4252a72004-07-30 07:50:03 +00004284 InsertNewInstBefore(Or, I);
Chris Lattner6e0123b2007-02-11 01:23:03 +00004285 Or->takeName(Op0);
Zhou Sheng9bc8ab12007-04-02 13:45:30 +00004286 return BinaryOperator::createAnd(Or,
4287 ConstantInt::get(RHS->getValue() | C1->getValue()));
Chris Lattnerd4252a72004-07-30 07:50:03 +00004288 }
Chris Lattner8f0d1562003-07-23 18:29:44 +00004289
Chris Lattnerd4252a72004-07-30 07:50:03 +00004290 // (X ^ C1) | C2 --> (X | C2) ^ (C1&~C2)
4291 if (match(Op0, m_Xor(m_Value(X), m_ConstantInt(C1))) && isOnlyUse(Op0)) {
Chris Lattner6e0123b2007-02-11 01:23:03 +00004292 Instruction *Or = BinaryOperator::createOr(X, RHS);
Chris Lattnerd4252a72004-07-30 07:50:03 +00004293 InsertNewInstBefore(Or, I);
Chris Lattner6e0123b2007-02-11 01:23:03 +00004294 Or->takeName(Op0);
Chris Lattnerd4252a72004-07-30 07:50:03 +00004295 return BinaryOperator::createXor(Or,
Zhou Sheng9bc8ab12007-04-02 13:45:30 +00004296 ConstantInt::get(C1->getValue() & ~RHS->getValue()));
Chris Lattner8f0d1562003-07-23 18:29:44 +00004297 }
Chris Lattner183b3362004-04-09 19:05:30 +00004298
4299 // Try to fold constant and into select arguments.
4300 if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
Chris Lattner86102b82005-01-01 16:22:27 +00004301 if (Instruction *R = FoldOpIntoSelect(I, SI, this))
Chris Lattner183b3362004-04-09 19:05:30 +00004302 return R;
Chris Lattner6a4adcd2004-09-29 05:07:12 +00004303 if (isa<PHINode>(Op0))
4304 if (Instruction *NV = FoldOpIntoPhi(I))
4305 return NV;
Chris Lattner8f0d1562003-07-23 18:29:44 +00004306 }
4307
Chris Lattner330628a2006-01-06 17:59:59 +00004308 Value *A = 0, *B = 0;
4309 ConstantInt *C1 = 0, *C2 = 0;
Chris Lattner4294cec2005-05-07 23:49:08 +00004310
4311 if (match(Op0, m_And(m_Value(A), m_Value(B))))
4312 if (A == Op1 || B == Op1) // (A & ?) | A --> A
4313 return ReplaceInstUsesWith(I, Op1);
4314 if (match(Op1, m_And(m_Value(A), m_Value(B))))
4315 if (A == Op0 || B == Op0) // A | (A & ?) --> A
4316 return ReplaceInstUsesWith(I, Op0);
4317
Chris Lattnerb7845d62006-07-10 20:25:24 +00004318 // (A | B) | C and A | (B | C) -> bswap if possible.
4319 // (A >> B) | (C << D) and (A << B) | (B >> C) -> bswap if possible.
Chris Lattnerc482a9e2006-06-15 19:07:26 +00004320 if (match(Op0, m_Or(m_Value(), m_Value())) ||
Chris Lattnerb7845d62006-07-10 20:25:24 +00004321 match(Op1, m_Or(m_Value(), m_Value())) ||
4322 (match(Op0, m_Shift(m_Value(), m_Value())) &&
4323 match(Op1, m_Shift(m_Value(), m_Value())))) {
Chris Lattnerc482a9e2006-06-15 19:07:26 +00004324 if (Instruction *BSwap = MatchBSwap(I))
4325 return BSwap;
4326 }
4327
Chris Lattnerb62f5082005-05-09 04:58:36 +00004328 // (X^C)|Y -> (X|Y)^C iff Y&C == 0
4329 if (Op0->hasOneUse() && match(Op0, m_Xor(m_Value(A), m_ConstantInt(C1))) &&
Reid Spencerb722f2b2007-03-22 22:19:58 +00004330 MaskedValueIsZero(Op1, C1->getValue())) {
Chris Lattner6e0123b2007-02-11 01:23:03 +00004331 Instruction *NOr = BinaryOperator::createOr(A, Op1);
4332 InsertNewInstBefore(NOr, I);
4333 NOr->takeName(Op0);
4334 return BinaryOperator::createXor(NOr, C1);
Chris Lattnerb62f5082005-05-09 04:58:36 +00004335 }
4336
4337 // Y|(X^C) -> (X|Y)^C iff Y&C == 0
4338 if (Op1->hasOneUse() && match(Op1, m_Xor(m_Value(A), m_ConstantInt(C1))) &&
Reid Spencerb722f2b2007-03-22 22:19:58 +00004339 MaskedValueIsZero(Op0, C1->getValue())) {
Chris Lattner6e0123b2007-02-11 01:23:03 +00004340 Instruction *NOr = BinaryOperator::createOr(A, Op0);
4341 InsertNewInstBefore(NOr, I);
4342 NOr->takeName(Op0);
4343 return BinaryOperator::createXor(NOr, C1);
Chris Lattnerb62f5082005-05-09 04:58:36 +00004344 }
4345
Chris Lattner1150df92007-04-08 07:47:01 +00004346 // (A & C)|(B & D)
Chris Lattner09a33a42007-06-19 05:43:49 +00004347 Value *C = 0, *D = 0;
Chris Lattner1150df92007-04-08 07:47:01 +00004348 if (match(Op0, m_And(m_Value(A), m_Value(C))) &&
4349 match(Op1, m_And(m_Value(B), m_Value(D)))) {
Chris Lattner7621a032007-04-08 07:55:22 +00004350 Value *V1 = 0, *V2 = 0, *V3 = 0;
4351 C1 = dyn_cast<ConstantInt>(C);
4352 C2 = dyn_cast<ConstantInt>(D);
4353 if (C1 && C2) { // (A & C1)|(B & C2)
4354 // If we have: ((V + N) & C1) | (V & C2)
4355 // .. and C2 = ~C1 and C2 is 0+1+ and (N & C2) == 0
4356 // replace with V+N.
4357 if (C1->getValue() == ~C2->getValue()) {
4358 if ((C2->getValue() & (C2->getValue()+1)) == 0 && // C2 == 0+1+
4359 match(A, m_Add(m_Value(V1), m_Value(V2)))) {
4360 // Add commutes, try both ways.
4361 if (V1 == B && MaskedValueIsZero(V2, C2->getValue()))
4362 return ReplaceInstUsesWith(I, A);
4363 if (V2 == B && MaskedValueIsZero(V1, C2->getValue()))
4364 return ReplaceInstUsesWith(I, A);
4365 }
4366 // Or commutes, try both ways.
4367 if ((C1->getValue() & (C1->getValue()+1)) == 0 &&
4368 match(B, m_Add(m_Value(V1), m_Value(V2)))) {
4369 // Add commutes, try both ways.
4370 if (V1 == A && MaskedValueIsZero(V2, C1->getValue()))
4371 return ReplaceInstUsesWith(I, B);
4372 if (V2 == A && MaskedValueIsZero(V1, C1->getValue()))
4373 return ReplaceInstUsesWith(I, B);
4374 }
4375 }
Chris Lattnerc8d37882007-04-08 08:01:49 +00004376 V1 = 0; V2 = 0; V3 = 0;
Chris Lattner7621a032007-04-08 07:55:22 +00004377 }
4378
Chris Lattner1150df92007-04-08 07:47:01 +00004379 // Check to see if we have any common things being and'ed. If so, find the
4380 // terms for V1 & (V2|V3).
Chris Lattner1150df92007-04-08 07:47:01 +00004381 if (isOnlyUse(Op0) || isOnlyUse(Op1)) {
4382 if (A == B) // (A & C)|(A & D) == A & (C|D)
4383 V1 = A, V2 = C, V3 = D;
4384 else if (A == D) // (A & C)|(B & A) == A & (B|C)
4385 V1 = A, V2 = B, V3 = C;
4386 else if (C == B) // (A & C)|(C & D) == C & (A|D)
4387 V1 = C, V2 = A, V3 = D;
4388 else if (C == D) // (A & C)|(B & C) == C & (A|B)
4389 V1 = C, V2 = A, V3 = B;
4390
4391 if (V1) {
4392 Value *Or =
4393 InsertNewInstBefore(BinaryOperator::createOr(V2, V3, "tmp"), I);
4394 return BinaryOperator::createAnd(V1, Or);
Chris Lattner01f56c62005-09-18 06:02:59 +00004395 }
Chris Lattner1150df92007-04-08 07:47:01 +00004396 }
Chris Lattner15212982005-09-18 03:42:07 +00004397 }
Chris Lattnerf05d69a2006-11-14 07:46:50 +00004398
4399 // (X >> Z) | (Y >> Z) -> (X|Y) >> Z for all shifts.
Reid Spencer2341c222007-02-02 02:16:23 +00004400 if (BinaryOperator *SI1 = dyn_cast<BinaryOperator>(Op1)) {
4401 if (BinaryOperator *SI0 = dyn_cast<BinaryOperator>(Op0))
4402 if (SI0->isShift() && SI0->getOpcode() == SI1->getOpcode() &&
Chris Lattnerf05d69a2006-11-14 07:46:50 +00004403 SI0->getOperand(1) == SI1->getOperand(1) &&
4404 (SI0->hasOneUse() || SI1->hasOneUse())) {
4405 Instruction *NewOp =
4406 InsertNewInstBefore(BinaryOperator::createOr(SI0->getOperand(0),
4407 SI1->getOperand(0),
4408 SI0->getName()), I);
Reid Spencer2341c222007-02-02 02:16:23 +00004409 return BinaryOperator::create(SI1->getOpcode(), NewOp,
4410 SI1->getOperand(1));
Chris Lattnerf05d69a2006-11-14 07:46:50 +00004411 }
4412 }
Chris Lattner812aab72003-08-12 19:11:07 +00004413
Chris Lattnerd4252a72004-07-30 07:50:03 +00004414 if (match(Op0, m_Not(m_Value(A)))) { // ~A | Op1
4415 if (A == Op1) // ~A | A == -1
Chris Lattner37338922007-06-15 06:23:19 +00004416 return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType()));
Chris Lattnerd4252a72004-07-30 07:50:03 +00004417 } else {
4418 A = 0;
4419 }
Chris Lattner4294cec2005-05-07 23:49:08 +00004420 // Note, A is still live here!
Chris Lattnerd4252a72004-07-30 07:50:03 +00004421 if (match(Op1, m_Not(m_Value(B)))) { // Op0 | ~B
4422 if (Op0 == B)
Chris Lattner37338922007-06-15 06:23:19 +00004423 return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType()));
Chris Lattner3e327a42003-03-10 23:13:59 +00004424
Misha Brukman9c003d82004-07-30 12:50:08 +00004425 // (~A | ~B) == (~(A & B)) - De Morgan's Law
Chris Lattnerd4252a72004-07-30 07:50:03 +00004426 if (A && isOnlyUse(Op0) && isOnlyUse(Op1)) {
4427 Value *And = InsertNewInstBefore(BinaryOperator::createAnd(A, B,
4428 I.getName()+".demorgan"), I);
4429 return BinaryOperator::createNot(And);
4430 }
Chris Lattner3e327a42003-03-10 23:13:59 +00004431 }
Chris Lattner3082c5a2003-02-18 19:28:33 +00004432
Reid Spencer266e42b2006-12-23 06:05:41 +00004433 // (icmp1 A, B) | (icmp2 A, B) --> (icmp3 A, B)
4434 if (ICmpInst *RHS = dyn_cast<ICmpInst>(I.getOperand(1))) {
4435 if (Instruction *R = AssociativeOpt(I, FoldICmpLogical(*this, RHS)))
Chris Lattner3ac7c262003-08-13 20:16:26 +00004436 return R;
4437
Chris Lattnerdcf756e2004-09-28 22:33:08 +00004438 Value *LHSVal, *RHSVal;
4439 ConstantInt *LHSCst, *RHSCst;
Reid Spencer266e42b2006-12-23 06:05:41 +00004440 ICmpInst::Predicate LHSCC, RHSCC;
4441 if (match(Op0, m_ICmp(LHSCC, m_Value(LHSVal), m_ConstantInt(LHSCst))))
4442 if (match(RHS, m_ICmp(RHSCC, m_Value(RHSVal), m_ConstantInt(RHSCst))))
4443 if (LHSVal == RHSVal && // Found (X icmp C1) | (X icmp C2)
4444 // icmp [us][gl]e x, cst is folded to icmp [us][gl]t elsewhere.
4445 LHSCC != ICmpInst::ICMP_UGE && LHSCC != ICmpInst::ICMP_ULE &&
4446 RHSCC != ICmpInst::ICMP_UGE && RHSCC != ICmpInst::ICMP_ULE &&
4447 LHSCC != ICmpInst::ICMP_SGE && LHSCC != ICmpInst::ICMP_SLE &&
Chris Lattnerfe2b44d2007-05-11 05:55:56 +00004448 RHSCC != ICmpInst::ICMP_SGE && RHSCC != ICmpInst::ICMP_SLE &&
4449 // We can't fold (ugt x, C) | (sgt x, C2).
4450 PredicatesFoldable(LHSCC, RHSCC)) {
Chris Lattnerdcf756e2004-09-28 22:33:08 +00004451 // Ensure that the larger constant is on the RHS.
Reid Spencer266e42b2006-12-23 06:05:41 +00004452 ICmpInst *LHS = cast<ICmpInst>(Op0);
Chris Lattnerfe2b44d2007-05-11 05:55:56 +00004453 bool NeedsSwap;
4454 if (ICmpInst::isSignedPredicate(LHSCC))
Chris Lattner600db3e2007-05-11 16:58:45 +00004455 NeedsSwap = LHSCst->getValue().sgt(RHSCst->getValue());
Chris Lattnerfe2b44d2007-05-11 05:55:56 +00004456 else
Chris Lattner600db3e2007-05-11 16:58:45 +00004457 NeedsSwap = LHSCst->getValue().ugt(RHSCst->getValue());
Chris Lattnerfe2b44d2007-05-11 05:55:56 +00004458
4459 if (NeedsSwap) {
Chris Lattnerdcf756e2004-09-28 22:33:08 +00004460 std::swap(LHS, RHS);
4461 std::swap(LHSCst, RHSCst);
4462 std::swap(LHSCC, RHSCC);
4463 }
4464
Reid Spencer266e42b2006-12-23 06:05:41 +00004465 // At this point, we know we have have two icmp instructions
Chris Lattnerdcf756e2004-09-28 22:33:08 +00004466 // comparing a value against two constants and or'ing the result
4467 // together. Because of the above check, we know that we only have
Reid Spencer266e42b2006-12-23 06:05:41 +00004468 // ICMP_EQ, ICMP_NE, ICMP_LT, and ICMP_GT here. We also know (from the
4469 // FoldICmpLogical check above), that the two constants are not
Chris Lattnerdcf756e2004-09-28 22:33:08 +00004470 // equal.
4471 assert(LHSCst != RHSCst && "Compares not folded above?");
4472
4473 switch (LHSCC) {
4474 default: assert(0 && "Unknown integer condition code!");
Reid Spencer266e42b2006-12-23 06:05:41 +00004475 case ICmpInst::ICMP_EQ:
Chris Lattnerdcf756e2004-09-28 22:33:08 +00004476 switch (RHSCC) {
4477 default: assert(0 && "Unknown integer condition code!");
Reid Spencer266e42b2006-12-23 06:05:41 +00004478 case ICmpInst::ICMP_EQ:
Chris Lattnerdcf756e2004-09-28 22:33:08 +00004479 if (LHSCst == SubOne(RHSCst)) {// (X == 13 | X == 14) -> X-13 <u 2
4480 Constant *AddCST = ConstantExpr::getNeg(LHSCst);
4481 Instruction *Add = BinaryOperator::createAdd(LHSVal, AddCST,
4482 LHSVal->getName()+".off");
4483 InsertNewInstBefore(Add, I);
Zhou Sheng9bc8ab12007-04-02 13:45:30 +00004484 AddCST = Subtract(AddOne(RHSCst), LHSCst);
Reid Spencer266e42b2006-12-23 06:05:41 +00004485 return new ICmpInst(ICmpInst::ICMP_ULT, Add, AddCST);
Chris Lattnerdcf756e2004-09-28 22:33:08 +00004486 }
Reid Spencer266e42b2006-12-23 06:05:41 +00004487 break; // (X == 13 | X == 15) -> no change
4488 case ICmpInst::ICMP_UGT: // (X == 13 | X u> 14) -> no change
4489 case ICmpInst::ICMP_SGT: // (X == 13 | X s> 14) -> no change
Chris Lattner5c219462005-04-19 06:04:18 +00004490 break;
Reid Spencer266e42b2006-12-23 06:05:41 +00004491 case ICmpInst::ICMP_NE: // (X == 13 | X != 15) -> X != 15
4492 case ICmpInst::ICMP_ULT: // (X == 13 | X u< 15) -> X u< 15
4493 case ICmpInst::ICMP_SLT: // (X == 13 | X s< 15) -> X s< 15
Chris Lattnerdcf756e2004-09-28 22:33:08 +00004494 return ReplaceInstUsesWith(I, RHS);
4495 }
4496 break;
Reid Spencer266e42b2006-12-23 06:05:41 +00004497 case ICmpInst::ICMP_NE:
Chris Lattnerdcf756e2004-09-28 22:33:08 +00004498 switch (RHSCC) {
4499 default: assert(0 && "Unknown integer condition code!");
Reid Spencer266e42b2006-12-23 06:05:41 +00004500 case ICmpInst::ICMP_EQ: // (X != 13 | X == 15) -> X != 13
4501 case ICmpInst::ICMP_UGT: // (X != 13 | X u> 15) -> X != 13
4502 case ICmpInst::ICMP_SGT: // (X != 13 | X s> 15) -> X != 13
Chris Lattnerdcf756e2004-09-28 22:33:08 +00004503 return ReplaceInstUsesWith(I, LHS);
Reid Spencer266e42b2006-12-23 06:05:41 +00004504 case ICmpInst::ICMP_NE: // (X != 13 | X != 15) -> true
4505 case ICmpInst::ICMP_ULT: // (X != 13 | X u< 15) -> true
4506 case ICmpInst::ICMP_SLT: // (X != 13 | X s< 15) -> true
Zhou Sheng75b871f2007-01-11 12:24:14 +00004507 return ReplaceInstUsesWith(I, ConstantInt::getTrue());
Chris Lattnerdcf756e2004-09-28 22:33:08 +00004508 }
4509 break;
Reid Spencer266e42b2006-12-23 06:05:41 +00004510 case ICmpInst::ICMP_ULT:
Chris Lattnerdcf756e2004-09-28 22:33:08 +00004511 switch (RHSCC) {
4512 default: assert(0 && "Unknown integer condition code!");
Reid Spencer266e42b2006-12-23 06:05:41 +00004513 case ICmpInst::ICMP_EQ: // (X u< 13 | X == 14) -> no change
Chris Lattnerdcf756e2004-09-28 22:33:08 +00004514 break;
Reid Spencer266e42b2006-12-23 06:05:41 +00004515 case ICmpInst::ICMP_UGT: // (X u< 13 | X u> 15) ->(X-13) u> 2
Chris Lattner74709472007-11-01 02:18:41 +00004516 // If RHSCst is [us]MAXINT, it is always false. Not handling
4517 // this can cause overflow.
4518 if (RHSCst->isMaxValue(false))
4519 return ReplaceInstUsesWith(I, LHS);
Reid Spencer266e42b2006-12-23 06:05:41 +00004520 return InsertRangeTest(LHSVal, LHSCst, AddOne(RHSCst), false,
4521 false, I);
4522 case ICmpInst::ICMP_SGT: // (X u< 13 | X s> 15) -> no change
4523 break;
4524 case ICmpInst::ICMP_NE: // (X u< 13 | X != 15) -> X != 15
4525 case ICmpInst::ICMP_ULT: // (X u< 13 | X u< 15) -> X u< 15
Chris Lattnerdcf756e2004-09-28 22:33:08 +00004526 return ReplaceInstUsesWith(I, RHS);
Reid Spencer266e42b2006-12-23 06:05:41 +00004527 case ICmpInst::ICMP_SLT: // (X u< 13 | X s< 15) -> no change
4528 break;
Chris Lattnerdcf756e2004-09-28 22:33:08 +00004529 }
4530 break;
Reid Spencer266e42b2006-12-23 06:05:41 +00004531 case ICmpInst::ICMP_SLT:
Chris Lattnerdcf756e2004-09-28 22:33:08 +00004532 switch (RHSCC) {
4533 default: assert(0 && "Unknown integer condition code!");
Reid Spencer266e42b2006-12-23 06:05:41 +00004534 case ICmpInst::ICMP_EQ: // (X s< 13 | X == 14) -> no change
4535 break;
4536 case ICmpInst::ICMP_SGT: // (X s< 13 | X s> 15) ->(X-13) s> 2
Chris Lattner74709472007-11-01 02:18:41 +00004537 // If RHSCst is [us]MAXINT, it is always false. Not handling
4538 // this can cause overflow.
4539 if (RHSCst->isMaxValue(true))
4540 return ReplaceInstUsesWith(I, LHS);
Reid Spencer266e42b2006-12-23 06:05:41 +00004541 return InsertRangeTest(LHSVal, LHSCst, AddOne(RHSCst), true,
4542 false, I);
4543 case ICmpInst::ICMP_UGT: // (X s< 13 | X u> 15) -> no change
4544 break;
4545 case ICmpInst::ICMP_NE: // (X s< 13 | X != 15) -> X != 15
4546 case ICmpInst::ICMP_SLT: // (X s< 13 | X s< 15) -> X s< 15
4547 return ReplaceInstUsesWith(I, RHS);
4548 case ICmpInst::ICMP_ULT: // (X s< 13 | X u< 15) -> no change
4549 break;
Chris Lattnerdcf756e2004-09-28 22:33:08 +00004550 }
Reid Spencer266e42b2006-12-23 06:05:41 +00004551 break;
4552 case ICmpInst::ICMP_UGT:
4553 switch (RHSCC) {
4554 default: assert(0 && "Unknown integer condition code!");
4555 case ICmpInst::ICMP_EQ: // (X u> 13 | X == 15) -> X u> 13
4556 case ICmpInst::ICMP_UGT: // (X u> 13 | X u> 15) -> X u> 13
4557 return ReplaceInstUsesWith(I, LHS);
4558 case ICmpInst::ICMP_SGT: // (X u> 13 | X s> 15) -> no change
4559 break;
4560 case ICmpInst::ICMP_NE: // (X u> 13 | X != 15) -> true
4561 case ICmpInst::ICMP_ULT: // (X u> 13 | X u< 15) -> true
Zhou Sheng75b871f2007-01-11 12:24:14 +00004562 return ReplaceInstUsesWith(I, ConstantInt::getTrue());
Reid Spencer266e42b2006-12-23 06:05:41 +00004563 case ICmpInst::ICMP_SLT: // (X u> 13 | X s< 15) -> no change
4564 break;
4565 }
4566 break;
4567 case ICmpInst::ICMP_SGT:
4568 switch (RHSCC) {
4569 default: assert(0 && "Unknown integer condition code!");
4570 case ICmpInst::ICMP_EQ: // (X s> 13 | X == 15) -> X > 13
4571 case ICmpInst::ICMP_SGT: // (X s> 13 | X s> 15) -> X > 13
4572 return ReplaceInstUsesWith(I, LHS);
4573 case ICmpInst::ICMP_UGT: // (X s> 13 | X u> 15) -> no change
4574 break;
4575 case ICmpInst::ICMP_NE: // (X s> 13 | X != 15) -> true
4576 case ICmpInst::ICMP_SLT: // (X s> 13 | X s< 15) -> true
Zhou Sheng75b871f2007-01-11 12:24:14 +00004577 return ReplaceInstUsesWith(I, ConstantInt::getTrue());
Reid Spencer266e42b2006-12-23 06:05:41 +00004578 case ICmpInst::ICMP_ULT: // (X s> 13 | X u< 15) -> no change
4579 break;
4580 }
4581 break;
Chris Lattnerdcf756e2004-09-28 22:33:08 +00004582 }
4583 }
4584 }
Chris Lattner3af10532006-05-05 06:39:07 +00004585
4586 // fold (or (cast A), (cast B)) -> (cast (or A, B))
Chris Lattnerc62877e2007-10-24 05:38:08 +00004587 if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) {
Chris Lattner3af10532006-05-05 06:39:07 +00004588 if (CastInst *Op1C = dyn_cast<CastInst>(Op1))
Reid Spencer799b5bf2006-12-13 08:27:15 +00004589 if (Op0C->getOpcode() == Op1C->getOpcode()) {// same cast kind ?
Evan Chengc3cf9f82008-03-24 00:21:34 +00004590 if (!isa<ICmpInst>(Op0C->getOperand(0)) ||
4591 !isa<ICmpInst>(Op1C->getOperand(0))) {
4592 const Type *SrcTy = Op0C->getOperand(0)->getType();
4593 if (SrcTy == Op1C->getOperand(0)->getType() && SrcTy->isInteger() &&
4594 // Only do this if the casts both really cause code to be
4595 // generated.
4596 ValueRequiresCast(Op0C->getOpcode(), Op0C->getOperand(0),
4597 I.getType(), TD) &&
4598 ValueRequiresCast(Op1C->getOpcode(), Op1C->getOperand(0),
4599 I.getType(), TD)) {
4600 Instruction *NewOp = BinaryOperator::createOr(Op0C->getOperand(0),
4601 Op1C->getOperand(0),
4602 I.getName());
4603 InsertNewInstBefore(NewOp, I);
4604 return CastInst::create(Op0C->getOpcode(), NewOp, I.getType());
4605 }
Reid Spencer799b5bf2006-12-13 08:27:15 +00004606 }
Chris Lattner3af10532006-05-05 06:39:07 +00004607 }
Chris Lattnerc62877e2007-10-24 05:38:08 +00004608 }
4609
4610
4611 // (fcmp uno x, c) | (fcmp uno y, c) -> (fcmp uno x, y)
4612 if (FCmpInst *LHS = dyn_cast<FCmpInst>(I.getOperand(0))) {
4613 if (FCmpInst *RHS = dyn_cast<FCmpInst>(I.getOperand(1))) {
4614 if (LHS->getPredicate() == FCmpInst::FCMP_UNO &&
Chris Lattnerc6125712008-02-29 06:09:11 +00004615 RHS->getPredicate() == FCmpInst::FCMP_UNO &&
4616 LHS->getOperand(0)->getType() == RHS->getOperand(0)->getType())
Chris Lattnerc62877e2007-10-24 05:38:08 +00004617 if (ConstantFP *LHSC = dyn_cast<ConstantFP>(LHS->getOperand(1)))
4618 if (ConstantFP *RHSC = dyn_cast<ConstantFP>(RHS->getOperand(1))) {
4619 // If either of the constants are nans, then the whole thing returns
4620 // true.
Chris Lattner55b83022007-10-24 18:54:45 +00004621 if (LHSC->getValueAPF().isNaN() || RHSC->getValueAPF().isNaN())
Chris Lattnerc62877e2007-10-24 05:38:08 +00004622 return ReplaceInstUsesWith(I, ConstantInt::getTrue());
4623
4624 // Otherwise, no need to compare the two constants, compare the
4625 // rest.
4626 return new FCmpInst(FCmpInst::FCMP_UNO, LHS->getOperand(0),
4627 RHS->getOperand(0));
4628 }
4629 }
4630 }
Chris Lattner15212982005-09-18 03:42:07 +00004631
Chris Lattner113f4f42002-06-25 16:13:24 +00004632 return Changed ? &I : 0;
Chris Lattnerf4cdbf32002-05-06 16:14:14 +00004633}
4634
Chris Lattnerc2076352004-02-16 01:20:27 +00004635// XorSelf - Implements: X ^ X --> 0
4636struct XorSelf {
4637 Value *RHS;
4638 XorSelf(Value *rhs) : RHS(rhs) {}
4639 bool shouldApply(Value *LHS) const { return LHS == RHS; }
4640 Instruction *apply(BinaryOperator &Xor) const {
4641 return &Xor;
4642 }
4643};
Chris Lattnerf4cdbf32002-05-06 16:14:14 +00004644
4645
Chris Lattner113f4f42002-06-25 16:13:24 +00004646Instruction *InstCombiner::visitXor(BinaryOperator &I) {
Chris Lattnerdcf240a2003-03-10 21:43:22 +00004647 bool Changed = SimplifyCommutative(I);
Chris Lattner113f4f42002-06-25 16:13:24 +00004648 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
Chris Lattnerf4cdbf32002-05-06 16:14:14 +00004649
Evan Cheng2b72c052008-03-25 20:07:13 +00004650 if (isa<UndefValue>(Op1)) {
4651 if (isa<UndefValue>(Op0))
4652 // Handle undef ^ undef -> 0 special case. This is a common
4653 // idiom (misuse).
4654 return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
Chris Lattner81a7a232004-10-16 18:11:37 +00004655 return ReplaceInstUsesWith(I, Op1); // X ^ undef -> undef
Evan Cheng2b72c052008-03-25 20:07:13 +00004656 }
Chris Lattner81a7a232004-10-16 18:11:37 +00004657
Chris Lattnerc2076352004-02-16 01:20:27 +00004658 // xor X, X = 0, even if X is nested in a sequence of Xor's.
4659 if (Instruction *Result = AssociativeOpt(I, XorSelf(Op1))) {
Chris Lattnerf0da7972007-08-05 08:47:58 +00004660 assert(Result == &I && "AssociativeOpt didn't work?"); Result=Result;
Chris Lattnere6794492002-08-12 21:17:25 +00004661 return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
Chris Lattnerc2076352004-02-16 01:20:27 +00004662 }
Chris Lattner5b2edb12006-02-12 08:02:11 +00004663
4664 // See if we can simplify any instructions used by the instruction whose sole
4665 // purpose is to compute bits we don't care about.
Reid Spencerb722f2b2007-03-22 22:19:58 +00004666 if (!isa<VectorType>(I.getType())) {
4667 uint32_t BitWidth = cast<IntegerType>(I.getType())->getBitWidth();
4668 APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0);
4669 if (SimplifyDemandedBits(&I, APInt::getAllOnesValue(BitWidth),
4670 KnownZero, KnownOne))
4671 return &I;
Chris Lattnerf14e5172007-06-15 05:26:55 +00004672 } else if (isa<ConstantAggregateZero>(Op1)) {
4673 return ReplaceInstUsesWith(I, Op0); // X ^ <0,0> -> X
Reid Spencerb722f2b2007-03-22 22:19:58 +00004674 }
Chris Lattnerf4cdbf32002-05-06 16:14:14 +00004675
Chris Lattner37338922007-06-15 06:23:19 +00004676 // Is this a ~ operation?
4677 if (Value *NotOp = dyn_castNotVal(&I)) {
4678 // ~(~X & Y) --> (X | ~Y) - De Morgan's Law
4679 // ~(~X | Y) === (X & ~Y) - De Morgan's Law
4680 if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(NotOp)) {
4681 if (Op0I->getOpcode() == Instruction::And ||
4682 Op0I->getOpcode() == Instruction::Or) {
4683 if (dyn_castNotVal(Op0I->getOperand(1))) Op0I->swapOperands();
4684 if (Value *Op0NotVal = dyn_castNotVal(Op0I->getOperand(0))) {
4685 Instruction *NotY =
4686 BinaryOperator::createNot(Op0I->getOperand(1),
4687 Op0I->getOperand(1)->getName()+".not");
4688 InsertNewInstBefore(NotY, I);
4689 if (Op0I->getOpcode() == Instruction::And)
4690 return BinaryOperator::createOr(Op0NotVal, NotY);
4691 else
4692 return BinaryOperator::createAnd(Op0NotVal, NotY);
4693 }
4694 }
4695 }
4696 }
4697
4698
Zhou Sheng75b871f2007-01-11 12:24:14 +00004699 if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) {
Nick Lewycky80520192007-08-06 20:04:16 +00004700 // xor (cmp A, B), true = not (cmp A, B) = !cmp A, B
4701 if (RHS == ConstantInt::getTrue() && Op0->hasOneUse()) {
4702 if (ICmpInst *ICI = dyn_cast<ICmpInst>(Op0))
Reid Spencer266e42b2006-12-23 06:05:41 +00004703 return new ICmpInst(ICI->getInversePredicate(),
4704 ICI->getOperand(0), ICI->getOperand(1));
Chris Lattnere5806662003-11-04 23:50:51 +00004705
Nick Lewycky80520192007-08-06 20:04:16 +00004706 if (FCmpInst *FCI = dyn_cast<FCmpInst>(Op0))
4707 return new FCmpInst(FCI->getInversePredicate(),
4708 FCI->getOperand(0), FCI->getOperand(1));
4709 }
4710
Reid Spencer266e42b2006-12-23 06:05:41 +00004711 if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) {
Chris Lattner8f2f5982003-11-05 01:06:05 +00004712 // ~(c-X) == X-c-1 == X+(-c-1)
Chris Lattnerc1e7cc02004-01-12 19:35:11 +00004713 if (Op0I->getOpcode() == Instruction::Sub && RHS->isAllOnesValue())
4714 if (Constant *Op0I0C = dyn_cast<Constant>(Op0I->getOperand(0))) {
Chris Lattnerdf20a4d2004-06-10 02:07:29 +00004715 Constant *NegOp0I0C = ConstantExpr::getNeg(Op0I0C);
4716 Constant *ConstantRHS = ConstantExpr::getSub(NegOp0I0C,
Chris Lattnerc1e7cc02004-01-12 19:35:11 +00004717 ConstantInt::get(I.getType(), 1));
Chris Lattnerdf20a4d2004-06-10 02:07:29 +00004718 return BinaryOperator::createAdd(Op0I->getOperand(1), ConstantRHS);
Chris Lattnerc1e7cc02004-01-12 19:35:11 +00004719 }
Chris Lattnerb24acc72007-04-02 05:36:22 +00004720
Anton Korobeynikov1bfd1212008-02-20 11:26:25 +00004721 if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) {
Chris Lattner5b2edb12006-02-12 08:02:11 +00004722 if (Op0I->getOpcode() == Instruction::Add) {
Chris Lattner0f68fa62003-11-04 23:37:10 +00004723 // ~(X-c) --> (-c-1)-X
Chris Lattnerc1e7cc02004-01-12 19:35:11 +00004724 if (RHS->isAllOnesValue()) {
Chris Lattnerdf20a4d2004-06-10 02:07:29 +00004725 Constant *NegOp0CI = ConstantExpr::getNeg(Op0CI);
4726 return BinaryOperator::createSub(
4727 ConstantExpr::getSub(NegOp0CI,
Chris Lattnerc1e7cc02004-01-12 19:35:11 +00004728 ConstantInt::get(I.getType(), 1)),
Chris Lattner0f68fa62003-11-04 23:37:10 +00004729 Op0I->getOperand(0));
Chris Lattner50490d52007-04-02 05:42:22 +00004730 } else if (RHS->getValue().isSignBit()) {
Chris Lattnerb24acc72007-04-02 05:36:22 +00004731 // (X + C) ^ signbit -> (X + C + signbit)
4732 Constant *C = ConstantInt::get(RHS->getValue() + Op0CI->getValue());
4733 return BinaryOperator::createAdd(Op0I->getOperand(0), C);
Chris Lattner9d5aace2007-04-02 05:48:58 +00004734
Chris Lattnerc1e7cc02004-01-12 19:35:11 +00004735 }
Chris Lattnerf78df7c2006-02-26 19:57:54 +00004736 } else if (Op0I->getOpcode() == Instruction::Or) {
4737 // (X|C1)^C2 -> X^(C1|C2) iff X&~C1 == 0
Reid Spencerb722f2b2007-03-22 22:19:58 +00004738 if (MaskedValueIsZero(Op0I->getOperand(0), Op0CI->getValue())) {
Chris Lattnerf78df7c2006-02-26 19:57:54 +00004739 Constant *NewRHS = ConstantExpr::getOr(Op0CI, RHS);
4740 // Anything in both C1 and C2 is known to be zero, remove it from
4741 // NewRHS.
Zhou Sheng9bc8ab12007-04-02 13:45:30 +00004742 Constant *CommonBits = And(Op0CI, RHS);
Chris Lattnerf78df7c2006-02-26 19:57:54 +00004743 NewRHS = ConstantExpr::getAnd(NewRHS,
4744 ConstantExpr::getNot(CommonBits));
Chris Lattnerb15e2b12007-03-02 21:28:56 +00004745 AddToWorkList(Op0I);
Chris Lattnerf78df7c2006-02-26 19:57:54 +00004746 I.setOperand(0, Op0I->getOperand(0));
4747 I.setOperand(1, NewRHS);
4748 return &I;
4749 }
Chris Lattner97638592003-07-23 21:37:07 +00004750 }
Anton Korobeynikov1bfd1212008-02-20 11:26:25 +00004751 }
Chris Lattnerb8d6e402002-08-20 18:24:26 +00004752 }
Chris Lattner183b3362004-04-09 19:05:30 +00004753
4754 // Try to fold constant and into select arguments.
4755 if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
Chris Lattner86102b82005-01-01 16:22:27 +00004756 if (Instruction *R = FoldOpIntoSelect(I, SI, this))
Chris Lattner183b3362004-04-09 19:05:30 +00004757 return R;
Chris Lattner6a4adcd2004-09-29 05:07:12 +00004758 if (isa<PHINode>(Op0))
4759 if (Instruction *NV = FoldOpIntoPhi(I))
4760 return NV;
Chris Lattnerf4cdbf32002-05-06 16:14:14 +00004761 }
4762
Chris Lattnerbb74e222003-03-10 23:06:50 +00004763 if (Value *X = dyn_castNotVal(Op0)) // ~A ^ A == -1
Chris Lattner3082c5a2003-02-18 19:28:33 +00004764 if (X == Op1)
Chris Lattner37338922007-06-15 06:23:19 +00004765 return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType()));
Chris Lattner3082c5a2003-02-18 19:28:33 +00004766
Chris Lattnerbb74e222003-03-10 23:06:50 +00004767 if (Value *X = dyn_castNotVal(Op1)) // A ^ ~A == -1
Chris Lattner3082c5a2003-02-18 19:28:33 +00004768 if (X == Op0)
Chris Lattner37338922007-06-15 06:23:19 +00004769 return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType()));
Chris Lattner3082c5a2003-02-18 19:28:33 +00004770
Chris Lattner07418422007-03-18 22:51:34 +00004771
4772 BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1);
4773 if (Op1I) {
4774 Value *A, *B;
4775 if (match(Op1I, m_Or(m_Value(A), m_Value(B)))) {
4776 if (A == Op0) { // B^(B|A) == (A|B)^B
Chris Lattnerdcd07922006-04-01 08:03:55 +00004777 Op1I->swapOperands();
Chris Lattner1bbb7b62003-03-10 18:24:17 +00004778 I.swapOperands();
4779 std::swap(Op0, Op1);
Chris Lattner07418422007-03-18 22:51:34 +00004780 } else if (B == Op0) { // B^(A|B) == (A|B)^B
Chris Lattnerdcd07922006-04-01 08:03:55 +00004781 I.swapOperands(); // Simplified below.
Chris Lattner1bbb7b62003-03-10 18:24:17 +00004782 std::swap(Op0, Op1);
Misha Brukmanb1c93172005-04-21 23:48:37 +00004783 }
Chris Lattner07418422007-03-18 22:51:34 +00004784 } else if (match(Op1I, m_Xor(m_Value(A), m_Value(B)))) {
4785 if (Op0 == A) // A^(A^B) == B
4786 return ReplaceInstUsesWith(I, B);
4787 else if (Op0 == B) // A^(B^A) == B
4788 return ReplaceInstUsesWith(I, A);
4789 } else if (match(Op1I, m_And(m_Value(A), m_Value(B))) && Op1I->hasOneUse()){
Chris Lattner04277992007-04-01 05:36:37 +00004790 if (A == Op0) { // A^(A&B) -> A^(B&A)
Chris Lattnerdcd07922006-04-01 08:03:55 +00004791 Op1I->swapOperands();
Chris Lattner04277992007-04-01 05:36:37 +00004792 std::swap(A, B);
4793 }
Chris Lattner07418422007-03-18 22:51:34 +00004794 if (B == Op0) { // A^(B&A) -> (B&A)^A
Chris Lattnerdcd07922006-04-01 08:03:55 +00004795 I.swapOperands(); // Simplified below.
4796 std::swap(Op0, Op1);
4797 }
Chris Lattnerb36d9082004-02-16 03:54:20 +00004798 }
Chris Lattner07418422007-03-18 22:51:34 +00004799 }
4800
4801 BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0);
4802 if (Op0I) {
4803 Value *A, *B;
4804 if (match(Op0I, m_Or(m_Value(A), m_Value(B))) && Op0I->hasOneUse()) {
4805 if (A == Op1) // (B|A)^B == (A|B)^B
4806 std::swap(A, B);
4807 if (B == Op1) { // (A|B)^B == A & ~B
4808 Instruction *NotB =
4809 InsertNewInstBefore(BinaryOperator::createNot(Op1, "tmp"), I);
4810 return BinaryOperator::createAnd(A, NotB);
Chris Lattner1bbb7b62003-03-10 18:24:17 +00004811 }
Chris Lattner07418422007-03-18 22:51:34 +00004812 } else if (match(Op0I, m_Xor(m_Value(A), m_Value(B)))) {
4813 if (Op1 == A) // (A^B)^A == B
4814 return ReplaceInstUsesWith(I, B);
4815 else if (Op1 == B) // (B^A)^A == B
4816 return ReplaceInstUsesWith(I, A);
4817 } else if (match(Op0I, m_And(m_Value(A), m_Value(B))) && Op0I->hasOneUse()){
4818 if (A == Op1) // (A&B)^A -> (B&A)^A
4819 std::swap(A, B);
4820 if (B == Op1 && // (B&A)^A == ~B & A
Chris Lattner6cf49142006-04-01 22:05:01 +00004821 !isa<ConstantInt>(Op1)) { // Canonical form is (B&C)^C
Chris Lattner07418422007-03-18 22:51:34 +00004822 Instruction *N =
4823 InsertNewInstBefore(BinaryOperator::createNot(A, "tmp"), I);
Chris Lattnerdcd07922006-04-01 08:03:55 +00004824 return BinaryOperator::createAnd(N, Op1);
4825 }
Chris Lattner1bbb7b62003-03-10 18:24:17 +00004826 }
Chris Lattner07418422007-03-18 22:51:34 +00004827 }
4828
4829 // (X >> Z) ^ (Y >> Z) -> (X^Y) >> Z for all shifts.
4830 if (Op0I && Op1I && Op0I->isShift() &&
4831 Op0I->getOpcode() == Op1I->getOpcode() &&
4832 Op0I->getOperand(1) == Op1I->getOperand(1) &&
4833 (Op1I->hasOneUse() || Op1I->hasOneUse())) {
4834 Instruction *NewOp =
4835 InsertNewInstBefore(BinaryOperator::createXor(Op0I->getOperand(0),
4836 Op1I->getOperand(0),
4837 Op0I->getName()), I);
4838 return BinaryOperator::create(Op1I->getOpcode(), NewOp,
4839 Op1I->getOperand(1));
4840 }
4841
4842 if (Op0I && Op1I) {
4843 Value *A, *B, *C, *D;
4844 // (A & B)^(A | B) -> A ^ B
4845 if (match(Op0I, m_And(m_Value(A), m_Value(B))) &&
4846 match(Op1I, m_Or(m_Value(C), m_Value(D)))) {
4847 if ((A == C && B == D) || (A == D && B == C))
4848 return BinaryOperator::createXor(A, B);
4849 }
4850 // (A | B)^(A & B) -> A ^ B
4851 if (match(Op0I, m_Or(m_Value(A), m_Value(B))) &&
4852 match(Op1I, m_And(m_Value(C), m_Value(D)))) {
4853 if ((A == C && B == D) || (A == D && B == C))
4854 return BinaryOperator::createXor(A, B);
4855 }
4856
4857 // (A & B)^(C & D)
4858 if ((Op0I->hasOneUse() || Op1I->hasOneUse()) &&
4859 match(Op0I, m_And(m_Value(A), m_Value(B))) &&
4860 match(Op1I, m_And(m_Value(C), m_Value(D)))) {
4861 // (X & Y)^(X & Y) -> (Y^Z) & X
4862 Value *X = 0, *Y = 0, *Z = 0;
4863 if (A == C)
4864 X = A, Y = B, Z = D;
4865 else if (A == D)
4866 X = A, Y = B, Z = C;
4867 else if (B == C)
4868 X = B, Y = A, Z = D;
4869 else if (B == D)
4870 X = B, Y = A, Z = C;
4871
4872 if (X) {
4873 Instruction *NewOp =
4874 InsertNewInstBefore(BinaryOperator::createXor(Y, Z, Op0->getName()), I);
4875 return BinaryOperator::createAnd(NewOp, X);
4876 }
4877 }
4878 }
4879
Reid Spencer266e42b2006-12-23 06:05:41 +00004880 // (icmp1 A, B) ^ (icmp2 A, B) --> (icmp3 A, B)
4881 if (ICmpInst *RHS = dyn_cast<ICmpInst>(I.getOperand(1)))
4882 if (Instruction *R = AssociativeOpt(I, FoldICmpLogical(*this, RHS)))
Chris Lattner3ac7c262003-08-13 20:16:26 +00004883 return R;
4884
Chris Lattner3af10532006-05-05 06:39:07 +00004885 // fold (xor (cast A), (cast B)) -> (cast (xor A, B))
Chris Lattnerc62877e2007-10-24 05:38:08 +00004886 if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) {
Chris Lattner3af10532006-05-05 06:39:07 +00004887 if (CastInst *Op1C = dyn_cast<CastInst>(Op1))
Reid Spencer799b5bf2006-12-13 08:27:15 +00004888 if (Op0C->getOpcode() == Op1C->getOpcode()) { // same cast kind?
4889 const Type *SrcTy = Op0C->getOperand(0)->getType();
Chris Lattner03c49532007-01-15 02:27:26 +00004890 if (SrcTy == Op1C->getOperand(0)->getType() && SrcTy->isInteger() &&
Reid Spencer799b5bf2006-12-13 08:27:15 +00004891 // Only do this if the casts both really cause code to be generated.
Reid Spencer266e42b2006-12-23 06:05:41 +00004892 ValueRequiresCast(Op0C->getOpcode(), Op0C->getOperand(0),
4893 I.getType(), TD) &&
4894 ValueRequiresCast(Op1C->getOpcode(), Op1C->getOperand(0),
4895 I.getType(), TD)) {
Reid Spencer799b5bf2006-12-13 08:27:15 +00004896 Instruction *NewOp = BinaryOperator::createXor(Op0C->getOperand(0),
4897 Op1C->getOperand(0),
4898 I.getName());
4899 InsertNewInstBefore(NewOp, I);
4900 return CastInst::create(Op0C->getOpcode(), NewOp, I.getType());
4901 }
Chris Lattner3af10532006-05-05 06:39:07 +00004902 }
Chris Lattnerc62877e2007-10-24 05:38:08 +00004903 }
Chris Lattner113f4f42002-06-25 16:13:24 +00004904 return Changed ? &I : 0;
Chris Lattnerf4cdbf32002-05-06 16:14:14 +00004905}
4906
Chris Lattner6862fbd2004-09-29 17:40:11 +00004907/// AddWithOverflow - Compute Result = In1+In2, returning true if the result
4908/// overflowed for this type.
4909static bool AddWithOverflow(ConstantInt *&Result, ConstantInt *In1,
Reid Spencerf4071162007-03-21 23:19:50 +00004910 ConstantInt *In2, bool IsSigned = false) {
Zhou Sheng9bc8ab12007-04-02 13:45:30 +00004911 Result = cast<ConstantInt>(Add(In1, In2));
Chris Lattner6862fbd2004-09-29 17:40:11 +00004912
Reid Spencerf4071162007-03-21 23:19:50 +00004913 if (IsSigned)
4914 if (In2->getValue().isNegative())
4915 return Result->getValue().sgt(In1->getValue());
4916 else
4917 return Result->getValue().slt(In1->getValue());
4918 else
4919 return Result->getValue().ult(In1->getValue());
Chris Lattner6862fbd2004-09-29 17:40:11 +00004920}
4921
Chris Lattner0798af32005-01-13 20:14:25 +00004922/// EmitGEPOffset - Given a getelementptr instruction/constantexpr, emit the
4923/// code necessary to compute the offset from the base pointer (without adding
4924/// in the base pointer). Return the result as a signed integer of intptr size.
4925static Value *EmitGEPOffset(User *GEP, Instruction &I, InstCombiner &IC) {
4926 TargetData &TD = IC.getTargetData();
4927 gep_type_iterator GTI = gep_type_begin(GEP);
Reid Spencer266e42b2006-12-23 06:05:41 +00004928 const Type *IntPtrTy = TD.getIntPtrType();
4929 Value *Result = Constant::getNullValue(IntPtrTy);
Chris Lattner0798af32005-01-13 20:14:25 +00004930
4931 // Build a mask for high order bits.
Chris Lattnerc3a43932008-04-22 02:53:33 +00004932 unsigned IntPtrWidth = TD.getPointerSizeInBits();
Chris Lattner6e880872007-04-28 04:52:43 +00004933 uint64_t PtrSizeMask = ~0ULL >> (64-IntPtrWidth);
Chris Lattner0798af32005-01-13 20:14:25 +00004934
Chris Lattner0798af32005-01-13 20:14:25 +00004935 for (unsigned i = 1, e = GEP->getNumOperands(); i != e; ++i, ++GTI) {
4936 Value *Op = GEP->getOperand(i);
Duncan Sands44b87212007-11-01 20:53:16 +00004937 uint64_t Size = TD.getABITypeSize(GTI.getIndexedType()) & PtrSizeMask;
Chris Lattner6e880872007-04-28 04:52:43 +00004938 if (ConstantInt *OpC = dyn_cast<ConstantInt>(Op)) {
4939 if (OpC->isZero()) continue;
4940
4941 // Handle a struct index, which adds its field offset to the pointer.
4942 if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
4943 Size = TD.getStructLayout(STy)->getElementOffset(OpC->getZExtValue());
4944
4945 if (ConstantInt *RC = dyn_cast<ConstantInt>(Result))
4946 Result = ConstantInt::get(RC->getValue() + APInt(IntPtrWidth, Size));
Chris Lattneracbf6a42007-04-28 00:57:34 +00004947 else
Chris Lattner6e880872007-04-28 04:52:43 +00004948 Result = IC.InsertNewInstBefore(
4949 BinaryOperator::createAdd(Result,
4950 ConstantInt::get(IntPtrTy, Size),
4951 GEP->getName()+".offs"), I);
4952 continue;
Chris Lattneracbf6a42007-04-28 00:57:34 +00004953 }
Chris Lattner6e880872007-04-28 04:52:43 +00004954
4955 Constant *Scale = ConstantInt::get(IntPtrTy, Size);
4956 Constant *OC = ConstantExpr::getIntegerCast(OpC, IntPtrTy, true /*SExt*/);
4957 Scale = ConstantExpr::getMul(OC, Scale);
4958 if (Constant *RC = dyn_cast<Constant>(Result))
4959 Result = ConstantExpr::getAdd(RC, Scale);
4960 else {
4961 // Emit an add instruction.
4962 Result = IC.InsertNewInstBefore(
4963 BinaryOperator::createAdd(Result, Scale,
4964 GEP->getName()+".offs"), I);
Chris Lattneracbf6a42007-04-28 00:57:34 +00004965 }
Chris Lattner6e880872007-04-28 04:52:43 +00004966 continue;
Chris Lattner0798af32005-01-13 20:14:25 +00004967 }
Chris Lattner6e880872007-04-28 04:52:43 +00004968 // Convert to correct type.
4969 if (Op->getType() != IntPtrTy) {
4970 if (Constant *OpC = dyn_cast<Constant>(Op))
4971 Op = ConstantExpr::getSExt(OpC, IntPtrTy);
4972 else
4973 Op = IC.InsertNewInstBefore(new SExtInst(Op, IntPtrTy,
4974 Op->getName()+".c"), I);
4975 }
4976 if (Size != 1) {
4977 Constant *Scale = ConstantInt::get(IntPtrTy, Size);
4978 if (Constant *OpC = dyn_cast<Constant>(Op))
4979 Op = ConstantExpr::getMul(OpC, Scale);
4980 else // We'll let instcombine(mul) convert this to a shl if possible.
4981 Op = IC.InsertNewInstBefore(BinaryOperator::createMul(Op, Scale,
4982 GEP->getName()+".idx"), I);
4983 }
4984
4985 // Emit an add instruction.
4986 if (isa<Constant>(Op) && isa<Constant>(Result))
4987 Result = ConstantExpr::getAdd(cast<Constant>(Op),
4988 cast<Constant>(Result));
4989 else
4990 Result = IC.InsertNewInstBefore(BinaryOperator::createAdd(Op, Result,
4991 GEP->getName()+".offs"), I);
Chris Lattner0798af32005-01-13 20:14:25 +00004992 }
4993 return Result;
4994}
4995
Chris Lattnerc3a43932008-04-22 02:53:33 +00004996
4997/// EvaluateGEPOffsetExpression - Return an value that can be used to compare of
4998/// the *offset* implied by GEP to zero. For example, if we have &A[i], we want
4999/// to return 'i' for "icmp ne i, 0". Note that, in general, indices can be
5000/// complex, and scales are involved. The above expression would also be legal
5001/// to codegen as "icmp ne (i*4), 0" (assuming A is a pointer to i32). This
5002/// later form is less amenable to optimization though, and we are allowed to
5003/// generate the first by knowing that pointer arithmetic doesn't overflow.
5004///
5005/// If we can't emit an optimized form for this expression, this returns null.
5006///
5007static Value *EvaluateGEPOffsetExpression(User *GEP, Instruction &I,
5008 InstCombiner &IC) {
Chris Lattnerc3a43932008-04-22 02:53:33 +00005009 TargetData &TD = IC.getTargetData();
5010 gep_type_iterator GTI = gep_type_begin(GEP);
5011
5012 // Check to see if this gep only has a single variable index. If so, and if
5013 // any constant indices are a multiple of its scale, then we can compute this
5014 // in terms of the scale of the variable index. For example, if the GEP
5015 // implies an offset of "12 + i*4", then we can codegen this as "3 + i",
5016 // because the expression will cross zero at the same point.
5017 unsigned i, e = GEP->getNumOperands();
5018 int64_t Offset = 0;
5019 for (i = 1; i != e; ++i, ++GTI) {
5020 if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(i))) {
5021 // Compute the aggregate offset of constant indices.
5022 if (CI->isZero()) continue;
5023
5024 // Handle a struct index, which adds its field offset to the pointer.
5025 if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
5026 Offset += TD.getStructLayout(STy)->getElementOffset(CI->getZExtValue());
5027 } else {
5028 uint64_t Size = TD.getABITypeSize(GTI.getIndexedType());
5029 Offset += Size*CI->getSExtValue();
5030 }
5031 } else {
5032 // Found our variable index.
5033 break;
5034 }
5035 }
5036
5037 // If there are no variable indices, we must have a constant offset, just
5038 // evaluate it the general way.
5039 if (i == e) return 0;
5040
5041 Value *VariableIdx = GEP->getOperand(i);
5042 // Determine the scale factor of the variable element. For example, this is
5043 // 4 if the variable index is into an array of i32.
5044 uint64_t VariableScale = TD.getABITypeSize(GTI.getIndexedType());
5045
5046 // Verify that there are no other variable indices. If so, emit the hard way.
5047 for (++i, ++GTI; i != e; ++i, ++GTI) {
5048 ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(i));
5049 if (!CI) return 0;
5050
5051 // Compute the aggregate offset of constant indices.
5052 if (CI->isZero()) continue;
5053
5054 // Handle a struct index, which adds its field offset to the pointer.
5055 if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
5056 Offset += TD.getStructLayout(STy)->getElementOffset(CI->getZExtValue());
5057 } else {
5058 uint64_t Size = TD.getABITypeSize(GTI.getIndexedType());
5059 Offset += Size*CI->getSExtValue();
5060 }
5061 }
5062
5063 // Okay, we know we have a single variable index, which must be a
5064 // pointer/array/vector index. If there is no offset, life is simple, return
5065 // the index.
5066 unsigned IntPtrWidth = TD.getPointerSizeInBits();
5067 if (Offset == 0) {
5068 // Cast to intptrty in case a truncation occurs. If an extension is needed,
5069 // we don't need to bother extending: the extension won't affect where the
5070 // computation crosses zero.
5071 if (VariableIdx->getType()->getPrimitiveSizeInBits() > IntPtrWidth)
5072 VariableIdx = new TruncInst(VariableIdx, TD.getIntPtrType(),
5073 VariableIdx->getNameStart(), &I);
5074 return VariableIdx;
5075 }
5076
5077 // Otherwise, there is an index. The computation we will do will be modulo
5078 // the pointer size, so get it.
5079 uint64_t PtrSizeMask = ~0ULL >> (64-IntPtrWidth);
5080
5081 Offset &= PtrSizeMask;
5082 VariableScale &= PtrSizeMask;
5083
5084 // To do this transformation, any constant index must be a multiple of the
5085 // variable scale factor. For example, we can evaluate "12 + 4*i" as "3 + i",
5086 // but we can't evaluate "10 + 3*i" in terms of i. Check that the offset is a
5087 // multiple of the variable scale.
5088 int64_t NewOffs = Offset / (int64_t)VariableScale;
5089 if (Offset != NewOffs*(int64_t)VariableScale)
5090 return 0;
5091
5092 // Okay, we can do this evaluation. Start by converting the index to intptr.
5093 const Type *IntPtrTy = TD.getIntPtrType();
5094 if (VariableIdx->getType() != IntPtrTy)
5095 VariableIdx = CastInst::createIntegerCast(VariableIdx, IntPtrTy,
5096 true /*SExt*/,
5097 VariableIdx->getNameStart(), &I);
5098 Constant *OffsetVal = ConstantInt::get(IntPtrTy, NewOffs);
5099 return BinaryOperator::createAdd(VariableIdx, OffsetVal, "offset", &I);
5100}
5101
5102
Reid Spencer266e42b2006-12-23 06:05:41 +00005103/// FoldGEPICmp - Fold comparisons between a GEP instruction and something
Chris Lattner0798af32005-01-13 20:14:25 +00005104/// else. At this point we know that the GEP is on the LHS of the comparison.
Reid Spencer266e42b2006-12-23 06:05:41 +00005105Instruction *InstCombiner::FoldGEPICmp(User *GEPLHS, Value *RHS,
5106 ICmpInst::Predicate Cond,
5107 Instruction &I) {
Chris Lattner0798af32005-01-13 20:14:25 +00005108 assert(dyn_castGetElementPtr(GEPLHS) && "LHS is not a getelementptr!");
Chris Lattner81e84172005-01-13 22:25:21 +00005109
Chris Lattnerc3a43932008-04-22 02:53:33 +00005110 // Look through bitcasts.
5111 if (BitCastInst *BCI = dyn_cast<BitCastInst>(RHS))
5112 RHS = BCI->getOperand(0);
Chris Lattner81e84172005-01-13 22:25:21 +00005113
Chris Lattner0798af32005-01-13 20:14:25 +00005114 Value *PtrBase = GEPLHS->getOperand(0);
5115 if (PtrBase == RHS) {
Chris Lattner682a7dc2008-02-05 04:45:32 +00005116 // ((gep Ptr, OFFSET) cmp Ptr) ---> (OFFSET cmp 0).
Chris Lattnerc3a43932008-04-22 02:53:33 +00005117 // This transformation (ignoring the base and scales) is valid because we
5118 // know pointers can't overflow. See if we can output an optimized form.
5119 Value *Offset = EvaluateGEPOffsetExpression(GEPLHS, I, *this);
5120
5121 // If not, synthesize the offset the hard way.
5122 if (Offset == 0)
5123 Offset = EmitGEPOffset(GEPLHS, I, *this);
Chris Lattner682a7dc2008-02-05 04:45:32 +00005124 return new ICmpInst(ICmpInst::getSignedPredicate(Cond), Offset,
5125 Constant::getNullValue(Offset->getType()));
Chris Lattner0798af32005-01-13 20:14:25 +00005126 } else if (User *GEPRHS = dyn_castGetElementPtr(RHS)) {
Chris Lattnera21bf8d2005-04-25 20:17:30 +00005127 // If the base pointers are different, but the indices are the same, just
5128 // compare the base pointer.
5129 if (PtrBase != GEPRHS->getOperand(0)) {
5130 bool IndicesTheSame = GEPLHS->getNumOperands()==GEPRHS->getNumOperands();
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00005131 IndicesTheSame &= GEPLHS->getOperand(0)->getType() ==
Chris Lattnerbd43b9d2005-04-26 14:40:41 +00005132 GEPRHS->getOperand(0)->getType();
Chris Lattnera21bf8d2005-04-25 20:17:30 +00005133 if (IndicesTheSame)
5134 for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i)
5135 if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) {
5136 IndicesTheSame = false;
5137 break;
5138 }
5139
5140 // If all indices are the same, just compare the base pointers.
5141 if (IndicesTheSame)
Reid Spencer266e42b2006-12-23 06:05:41 +00005142 return new ICmpInst(ICmpInst::getSignedPredicate(Cond),
5143 GEPLHS->getOperand(0), GEPRHS->getOperand(0));
Chris Lattnera21bf8d2005-04-25 20:17:30 +00005144
5145 // Otherwise, the base pointers are different and the indices are
5146 // different, bail out.
Chris Lattner0798af32005-01-13 20:14:25 +00005147 return 0;
Chris Lattnera21bf8d2005-04-25 20:17:30 +00005148 }
Chris Lattner0798af32005-01-13 20:14:25 +00005149
Chris Lattner81e84172005-01-13 22:25:21 +00005150 // If one of the GEPs has all zero indices, recurse.
5151 bool AllZeros = true;
5152 for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i)
5153 if (!isa<Constant>(GEPLHS->getOperand(i)) ||
5154 !cast<Constant>(GEPLHS->getOperand(i))->isNullValue()) {
5155 AllZeros = false;
5156 break;
5157 }
5158 if (AllZeros)
Reid Spencer266e42b2006-12-23 06:05:41 +00005159 return FoldGEPICmp(GEPRHS, GEPLHS->getOperand(0),
5160 ICmpInst::getSwappedPredicate(Cond), I);
Chris Lattner4fa89822005-01-14 00:20:05 +00005161
5162 // If the other GEP has all zero indices, recurse.
Chris Lattner81e84172005-01-13 22:25:21 +00005163 AllZeros = true;
5164 for (unsigned i = 1, e = GEPRHS->getNumOperands(); i != e; ++i)
5165 if (!isa<Constant>(GEPRHS->getOperand(i)) ||
5166 !cast<Constant>(GEPRHS->getOperand(i))->isNullValue()) {
5167 AllZeros = false;
5168 break;
5169 }
5170 if (AllZeros)
Reid Spencer266e42b2006-12-23 06:05:41 +00005171 return FoldGEPICmp(GEPLHS, GEPRHS->getOperand(0), Cond, I);
Chris Lattner81e84172005-01-13 22:25:21 +00005172
Chris Lattner4fa89822005-01-14 00:20:05 +00005173 if (GEPLHS->getNumOperands() == GEPRHS->getNumOperands()) {
5174 // If the GEPs only differ by one index, compare it.
5175 unsigned NumDifferences = 0; // Keep track of # differences.
5176 unsigned DiffOperand = 0; // The operand that differs.
5177 for (unsigned i = 1, e = GEPRHS->getNumOperands(); i != e; ++i)
5178 if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) {
Chris Lattnerd1f46d32005-04-24 06:59:08 +00005179 if (GEPLHS->getOperand(i)->getType()->getPrimitiveSizeInBits() !=
5180 GEPRHS->getOperand(i)->getType()->getPrimitiveSizeInBits()) {
Chris Lattnerfc4429e2005-01-21 23:06:49 +00005181 // Irreconcilable differences.
Chris Lattner4fa89822005-01-14 00:20:05 +00005182 NumDifferences = 2;
5183 break;
5184 } else {
5185 if (NumDifferences++) break;
5186 DiffOperand = i;
5187 }
5188 }
5189
5190 if (NumDifferences == 0) // SAME GEP?
5191 return ReplaceInstUsesWith(I, // No comparison is needed here.
Nick Lewycky0c5c4792007-09-06 02:40:25 +00005192 ConstantInt::get(Type::Int1Ty,
5193 isTrueWhenEqual(Cond)));
5194
Chris Lattner4fa89822005-01-14 00:20:05 +00005195 else if (NumDifferences == 1) {
Chris Lattnerfc4429e2005-01-21 23:06:49 +00005196 Value *LHSV = GEPLHS->getOperand(DiffOperand);
5197 Value *RHSV = GEPRHS->getOperand(DiffOperand);
Reid Spencer266e42b2006-12-23 06:05:41 +00005198 // Make sure we do a signed comparison here.
5199 return new ICmpInst(ICmpInst::getSignedPredicate(Cond), LHSV, RHSV);
Chris Lattner4fa89822005-01-14 00:20:05 +00005200 }
5201 }
5202
Reid Spencer266e42b2006-12-23 06:05:41 +00005203 // Only lower this if the icmp is the only user of the GEP or if we expect
Chris Lattner0798af32005-01-13 20:14:25 +00005204 // the result to fold to a constant!
5205 if ((isa<ConstantExpr>(GEPLHS) || GEPLHS->hasOneUse()) &&
5206 (isa<ConstantExpr>(GEPRHS) || GEPRHS->hasOneUse())) {
5207 // ((gep Ptr, OFFSET1) cmp (gep Ptr, OFFSET2) ---> (OFFSET1 cmp OFFSET2)
5208 Value *L = EmitGEPOffset(GEPLHS, I, *this);
5209 Value *R = EmitGEPOffset(GEPRHS, I, *this);
Reid Spencer266e42b2006-12-23 06:05:41 +00005210 return new ICmpInst(ICmpInst::getSignedPredicate(Cond), L, R);
Chris Lattner0798af32005-01-13 20:14:25 +00005211 }
5212 }
5213 return 0;
5214}
5215
Reid Spencer266e42b2006-12-23 06:05:41 +00005216Instruction *InstCombiner::visitFCmpInst(FCmpInst &I) {
5217 bool Changed = SimplifyCompare(I);
Chris Lattner6d14f2a2002-08-09 23:47:40 +00005218 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
Chris Lattnerf4cdbf32002-05-06 16:14:14 +00005219
Chris Lattner6ee923f2007-01-14 19:42:17 +00005220 // Fold trivial predicates.
5221 if (I.getPredicate() == FCmpInst::FCMP_FALSE)
5222 return ReplaceInstUsesWith(I, Constant::getNullValue(Type::Int1Ty));
5223 if (I.getPredicate() == FCmpInst::FCMP_TRUE)
5224 return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, 1));
5225
5226 // Simplify 'fcmp pred X, X'
5227 if (Op0 == Op1) {
5228 switch (I.getPredicate()) {
5229 default: assert(0 && "Unknown predicate!");
5230 case FCmpInst::FCMP_UEQ: // True if unordered or equal
5231 case FCmpInst::FCMP_UGE: // True if unordered, greater than, or equal
5232 case FCmpInst::FCMP_ULE: // True if unordered, less than, or equal
5233 return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, 1));
5234 case FCmpInst::FCMP_OGT: // True if ordered and greater than
5235 case FCmpInst::FCMP_OLT: // True if ordered and less than
5236 case FCmpInst::FCMP_ONE: // True if ordered and operands are unequal
5237 return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, 0));
5238
5239 case FCmpInst::FCMP_UNO: // True if unordered: isnan(X) | isnan(Y)
5240 case FCmpInst::FCMP_ULT: // True if unordered or less than
5241 case FCmpInst::FCMP_UGT: // True if unordered or greater than
5242 case FCmpInst::FCMP_UNE: // True if unordered or not equal
5243 // Canonicalize these to be 'fcmp uno %X, 0.0'.
5244 I.setPredicate(FCmpInst::FCMP_UNO);
5245 I.setOperand(1, Constant::getNullValue(Op0->getType()));
5246 return &I;
5247
5248 case FCmpInst::FCMP_ORD: // True if ordered (no nans)
5249 case FCmpInst::FCMP_OEQ: // True if ordered and equal
5250 case FCmpInst::FCMP_OGE: // True if ordered and greater than or equal
5251 case FCmpInst::FCMP_OLE: // True if ordered and less than or equal
5252 // Canonicalize these to be 'fcmp ord %X, 0.0'.
5253 I.setPredicate(FCmpInst::FCMP_ORD);
5254 I.setOperand(1, Constant::getNullValue(Op0->getType()));
5255 return &I;
5256 }
5257 }
5258
Reid Spencer266e42b2006-12-23 06:05:41 +00005259 if (isa<UndefValue>(Op1)) // fcmp pred X, undef -> undef
Reid Spencer542964f2007-01-11 18:21:29 +00005260 return ReplaceInstUsesWith(I, UndefValue::get(Type::Int1Ty));
Chris Lattner81a7a232004-10-16 18:11:37 +00005261
Reid Spencer266e42b2006-12-23 06:05:41 +00005262 // Handle fcmp with constant RHS
5263 if (Constant *RHSC = dyn_cast<Constant>(Op1)) {
5264 if (Instruction *LHSI = dyn_cast<Instruction>(Op0))
5265 switch (LHSI->getOpcode()) {
5266 case Instruction::PHI:
5267 if (Instruction *NV = FoldOpIntoPhi(I))
5268 return NV;
5269 break;
5270 case Instruction::Select:
5271 // If either operand of the select is a constant, we can fold the
5272 // comparison into the select arms, which will cause one to be
5273 // constant folded and the select turned into a bitwise or.
5274 Value *Op1 = 0, *Op2 = 0;
5275 if (LHSI->hasOneUse()) {
5276 if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(1))) {
5277 // Fold the known value into the constant operand.
5278 Op1 = ConstantExpr::getCompare(I.getPredicate(), C, RHSC);
5279 // Insert a new FCmp of the other select operand.
5280 Op2 = InsertNewInstBefore(new FCmpInst(I.getPredicate(),
5281 LHSI->getOperand(2), RHSC,
5282 I.getName()), I);
5283 } else if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(2))) {
5284 // Fold the known value into the constant operand.
5285 Op2 = ConstantExpr::getCompare(I.getPredicate(), C, RHSC);
5286 // Insert a new FCmp of the other select operand.
5287 Op1 = InsertNewInstBefore(new FCmpInst(I.getPredicate(),
5288 LHSI->getOperand(1), RHSC,
5289 I.getName()), I);
5290 }
5291 }
5292
5293 if (Op1)
Gabor Greife9ecc682008-04-06 20:25:17 +00005294 return SelectInst::Create(LHSI->getOperand(0), Op1, Op2);
Reid Spencer266e42b2006-12-23 06:05:41 +00005295 break;
5296 }
5297 }
5298
5299 return Changed ? &I : 0;
5300}
5301
5302Instruction *InstCombiner::visitICmpInst(ICmpInst &I) {
5303 bool Changed = SimplifyCompare(I);
5304 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
5305 const Type *Ty = Op0->getType();
5306
5307 // icmp X, X
5308 if (Op0 == Op1)
Reid Spencercddc9df2007-01-12 04:24:46 +00005309 return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty,
5310 isTrueWhenEqual(I)));
Reid Spencer266e42b2006-12-23 06:05:41 +00005311
5312 if (isa<UndefValue>(Op1)) // X icmp undef -> undef
Reid Spencer542964f2007-01-11 18:21:29 +00005313 return ReplaceInstUsesWith(I, UndefValue::get(Type::Int1Ty));
Christopher Lambf00ac6d2007-12-18 21:32:20 +00005314
Reid Spencer266e42b2006-12-23 06:05:41 +00005315 // icmp <global/alloca*/null>, <global/alloca*/null> - Global/Stack value
Chris Lattner15ff1e12004-11-14 07:33:16 +00005316 // addresses never equal each other! We already know that Op0 != Op1.
Misha Brukmanb1c93172005-04-21 23:48:37 +00005317 if ((isa<GlobalValue>(Op0) || isa<AllocaInst>(Op0) ||
5318 isa<ConstantPointerNull>(Op0)) &&
5319 (isa<GlobalValue>(Op1) || isa<AllocaInst>(Op1) ||
Chris Lattner15ff1e12004-11-14 07:33:16 +00005320 isa<ConstantPointerNull>(Op1)))
Reid Spencercddc9df2007-01-12 04:24:46 +00005321 return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty,
5322 !isTrueWhenEqual(I)));
Chris Lattner6d14f2a2002-08-09 23:47:40 +00005323
Reid Spencer266e42b2006-12-23 06:05:41 +00005324 // icmp's with boolean values can always be turned into bitwise operations
Reid Spencer542964f2007-01-11 18:21:29 +00005325 if (Ty == Type::Int1Ty) {
Reid Spencer266e42b2006-12-23 06:05:41 +00005326 switch (I.getPredicate()) {
5327 default: assert(0 && "Invalid icmp instruction!");
5328 case ICmpInst::ICMP_EQ: { // icmp eq bool %A, %B -> ~(A^B)
Chris Lattnerdf20a4d2004-06-10 02:07:29 +00005329 Instruction *Xor = BinaryOperator::createXor(Op0, Op1, I.getName()+"tmp");
Chris Lattner6d14f2a2002-08-09 23:47:40 +00005330 InsertNewInstBefore(Xor, I);
Chris Lattner16930792003-11-03 04:25:02 +00005331 return BinaryOperator::createNot(Xor);
Chris Lattner6d14f2a2002-08-09 23:47:40 +00005332 }
Reid Spencer266e42b2006-12-23 06:05:41 +00005333 case ICmpInst::ICMP_NE: // icmp eq bool %A, %B -> A^B
Chris Lattner4456da62004-08-11 00:50:51 +00005334 return BinaryOperator::createXor(Op0, Op1);
Chris Lattner6d14f2a2002-08-09 23:47:40 +00005335
Reid Spencer266e42b2006-12-23 06:05:41 +00005336 case ICmpInst::ICMP_UGT:
5337 case ICmpInst::ICMP_SGT:
5338 std::swap(Op0, Op1); // Change icmp gt -> icmp lt
Chris Lattner4456da62004-08-11 00:50:51 +00005339 // FALL THROUGH
Reid Spencer266e42b2006-12-23 06:05:41 +00005340 case ICmpInst::ICMP_ULT:
5341 case ICmpInst::ICMP_SLT: { // icmp lt bool A, B -> ~X & Y
Chris Lattner4456da62004-08-11 00:50:51 +00005342 Instruction *Not = BinaryOperator::createNot(Op0, I.getName()+"tmp");
5343 InsertNewInstBefore(Not, I);
5344 return BinaryOperator::createAnd(Not, Op1);
5345 }
Reid Spencer266e42b2006-12-23 06:05:41 +00005346 case ICmpInst::ICMP_UGE:
5347 case ICmpInst::ICMP_SGE:
5348 std::swap(Op0, Op1); // Change icmp ge -> icmp le
Chris Lattner4456da62004-08-11 00:50:51 +00005349 // FALL THROUGH
Reid Spencer266e42b2006-12-23 06:05:41 +00005350 case ICmpInst::ICMP_ULE:
5351 case ICmpInst::ICMP_SLE: { // icmp le bool %A, %B -> ~A | B
Chris Lattner4456da62004-08-11 00:50:51 +00005352 Instruction *Not = BinaryOperator::createNot(Op0, I.getName()+"tmp");
5353 InsertNewInstBefore(Not, I);
5354 return BinaryOperator::createOr(Not, Op1);
5355 }
5356 }
Chris Lattner6d14f2a2002-08-09 23:47:40 +00005357 }
5358
Chris Lattner2dd01742004-06-09 04:24:29 +00005359 // See if we are doing a comparison between a constant and an instruction that
5360 // can be folded into the comparison.
Chris Lattner6d14f2a2002-08-09 23:47:40 +00005361 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
Christopher Lamb7d82bc42007-12-20 07:21:11 +00005362 Value *A, *B;
5363
Chris Lattnerdb026d72008-01-05 01:18:20 +00005364 // (icmp ne/eq (sub A B) 0) -> (icmp ne/eq A, B)
5365 if (I.isEquality() && CI->isNullValue() &&
5366 match(Op0, m_Sub(m_Value(A), m_Value(B)))) {
5367 // (icmp cond A B) if cond is equality
5368 return new ICmpInst(I.getPredicate(), A, B);
Owen Anderson73639142007-12-28 07:42:12 +00005369 }
Christopher Lamb7d82bc42007-12-20 07:21:11 +00005370
Reid Spencer266e42b2006-12-23 06:05:41 +00005371 switch (I.getPredicate()) {
5372 default: break;
5373 case ICmpInst::ICMP_ULT: // A <u MIN -> FALSE
5374 if (CI->isMinValue(false))
Zhou Sheng75b871f2007-01-11 12:24:14 +00005375 return ReplaceInstUsesWith(I, ConstantInt::getFalse());
Reid Spencer266e42b2006-12-23 06:05:41 +00005376 if (CI->isMaxValue(false)) // A <u MAX -> A != MAX
5377 return new ICmpInst(ICmpInst::ICMP_NE, Op0,Op1);
5378 if (isMinValuePlusOne(CI,false)) // A <u MIN+1 -> A == MIN
5379 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, SubOne(CI));
Chris Lattner20f23722007-04-11 06:12:58 +00005380 // (x <u 2147483648) -> (x >s -1) -> true if sign bit clear
5381 if (CI->isMinValue(true))
5382 return new ICmpInst(ICmpInst::ICMP_SGT, Op0,
5383 ConstantInt::getAllOnesValue(Op0->getType()));
5384
Reid Spencer266e42b2006-12-23 06:05:41 +00005385 break;
Chris Lattner6862fbd2004-09-29 17:40:11 +00005386
Reid Spencer266e42b2006-12-23 06:05:41 +00005387 case ICmpInst::ICMP_SLT:
5388 if (CI->isMinValue(true)) // A <s MIN -> FALSE
Zhou Sheng75b871f2007-01-11 12:24:14 +00005389 return ReplaceInstUsesWith(I, ConstantInt::getFalse());
Reid Spencer266e42b2006-12-23 06:05:41 +00005390 if (CI->isMaxValue(true)) // A <s MAX -> A != MAX
5391 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
5392 if (isMinValuePlusOne(CI,true)) // A <s MIN+1 -> A == MIN
5393 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, SubOne(CI));
5394 break;
5395
5396 case ICmpInst::ICMP_UGT:
5397 if (CI->isMaxValue(false)) // A >u MAX -> FALSE
Zhou Sheng75b871f2007-01-11 12:24:14 +00005398 return ReplaceInstUsesWith(I, ConstantInt::getFalse());
Reid Spencer266e42b2006-12-23 06:05:41 +00005399 if (CI->isMinValue(false)) // A >u MIN -> A != MIN
5400 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
5401 if (isMaxValueMinusOne(CI, false)) // A >u MAX-1 -> A == MAX
5402 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, AddOne(CI));
Chris Lattner20f23722007-04-11 06:12:58 +00005403
5404 // (x >u 2147483647) -> (x <s 0) -> true if sign bit set
5405 if (CI->isMaxValue(true))
5406 return new ICmpInst(ICmpInst::ICMP_SLT, Op0,
5407 ConstantInt::getNullValue(Op0->getType()));
Reid Spencer266e42b2006-12-23 06:05:41 +00005408 break;
5409
5410 case ICmpInst::ICMP_SGT:
5411 if (CI->isMaxValue(true)) // A >s MAX -> FALSE
Zhou Sheng75b871f2007-01-11 12:24:14 +00005412 return ReplaceInstUsesWith(I, ConstantInt::getFalse());
Reid Spencer266e42b2006-12-23 06:05:41 +00005413 if (CI->isMinValue(true)) // A >s MIN -> A != MIN
5414 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
5415 if (isMaxValueMinusOne(CI, true)) // A >s MAX-1 -> A == MAX
5416 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, AddOne(CI));
5417 break;
5418
5419 case ICmpInst::ICMP_ULE:
5420 if (CI->isMaxValue(false)) // A <=u MAX -> TRUE
Zhou Sheng75b871f2007-01-11 12:24:14 +00005421 return ReplaceInstUsesWith(I, ConstantInt::getTrue());
Reid Spencer266e42b2006-12-23 06:05:41 +00005422 if (CI->isMinValue(false)) // A <=u MIN -> A == MIN
5423 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1);
5424 if (isMaxValueMinusOne(CI,false)) // A <=u MAX-1 -> A != MAX
5425 return new ICmpInst(ICmpInst::ICMP_NE, Op0, AddOne(CI));
5426 break;
Chris Lattner6862fbd2004-09-29 17:40:11 +00005427
Reid Spencer266e42b2006-12-23 06:05:41 +00005428 case ICmpInst::ICMP_SLE:
5429 if (CI->isMaxValue(true)) // A <=s MAX -> TRUE
Zhou Sheng75b871f2007-01-11 12:24:14 +00005430 return ReplaceInstUsesWith(I, ConstantInt::getTrue());
Reid Spencer266e42b2006-12-23 06:05:41 +00005431 if (CI->isMinValue(true)) // A <=s MIN -> A == MIN
5432 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1);
5433 if (isMaxValueMinusOne(CI,true)) // A <=s MAX-1 -> A != MAX
5434 return new ICmpInst(ICmpInst::ICMP_NE, Op0, AddOne(CI));
5435 break;
Chris Lattner6862fbd2004-09-29 17:40:11 +00005436
Reid Spencer266e42b2006-12-23 06:05:41 +00005437 case ICmpInst::ICMP_UGE:
5438 if (CI->isMinValue(false)) // A >=u MIN -> TRUE
Zhou Sheng75b871f2007-01-11 12:24:14 +00005439 return ReplaceInstUsesWith(I, ConstantInt::getTrue());
Reid Spencer266e42b2006-12-23 06:05:41 +00005440 if (CI->isMaxValue(false)) // A >=u MAX -> A == MAX
5441 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1);
5442 if (isMinValuePlusOne(CI,false)) // A >=u MIN-1 -> A != MIN
5443 return new ICmpInst(ICmpInst::ICMP_NE, Op0, SubOne(CI));
5444 break;
5445
5446 case ICmpInst::ICMP_SGE:
5447 if (CI->isMinValue(true)) // A >=s MIN -> TRUE
Zhou Sheng75b871f2007-01-11 12:24:14 +00005448 return ReplaceInstUsesWith(I, ConstantInt::getTrue());
Reid Spencer266e42b2006-12-23 06:05:41 +00005449 if (CI->isMaxValue(true)) // A >=s MAX -> A == MAX
5450 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1);
5451 if (isMinValuePlusOne(CI,true)) // A >=s MIN-1 -> A != MIN
5452 return new ICmpInst(ICmpInst::ICMP_NE, Op0, SubOne(CI));
5453 break;
Chris Lattner6862fbd2004-09-29 17:40:11 +00005454 }
5455
Reid Spencer266e42b2006-12-23 06:05:41 +00005456 // If we still have a icmp le or icmp ge instruction, turn it into the
5457 // appropriate icmp lt or icmp gt instruction. Since the border cases have
Chris Lattner6862fbd2004-09-29 17:40:11 +00005458 // already been handled above, this requires little checking.
5459 //
Reid Spencer624766f2007-03-25 19:55:33 +00005460 switch (I.getPredicate()) {
Chris Lattnerd4fef8d2007-07-15 20:54:51 +00005461 default: break;
5462 case ICmpInst::ICMP_ULE:
5463 return new ICmpInst(ICmpInst::ICMP_ULT, Op0, AddOne(CI));
5464 case ICmpInst::ICMP_SLE:
5465 return new ICmpInst(ICmpInst::ICMP_SLT, Op0, AddOne(CI));
5466 case ICmpInst::ICMP_UGE:
5467 return new ICmpInst( ICmpInst::ICMP_UGT, Op0, SubOne(CI));
5468 case ICmpInst::ICMP_SGE:
5469 return new ICmpInst(ICmpInst::ICMP_SGT, Op0, SubOne(CI));
Reid Spencer624766f2007-03-25 19:55:33 +00005470 }
Chris Lattneree0f2802006-02-12 02:07:56 +00005471
5472 // See if we can fold the comparison based on bits known to be zero or one
Chris Lattnerd4fef8d2007-07-15 20:54:51 +00005473 // in the input. If this comparison is a normal comparison, it demands all
5474 // bits, if it is a sign bit comparison, it only demands the sign bit.
5475
5476 bool UnusedBit;
5477 bool isSignBit = isSignBitCheck(I.getPredicate(), CI, UnusedBit);
5478
Reid Spencerc3e3b8a2007-03-22 20:36:03 +00005479 uint32_t BitWidth = cast<IntegerType>(Ty)->getBitWidth();
5480 APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0);
Chris Lattnerd4fef8d2007-07-15 20:54:51 +00005481 if (SimplifyDemandedBits(Op0,
5482 isSignBit ? APInt::getSignBit(BitWidth)
5483 : APInt::getAllOnesValue(BitWidth),
Chris Lattneree0f2802006-02-12 02:07:56 +00005484 KnownZero, KnownOne, 0))
5485 return &I;
5486
5487 // Given the known and unknown bits, compute a range that the LHS could be
5488 // in.
Reid Spencerc3e3b8a2007-03-22 20:36:03 +00005489 if ((KnownOne | KnownZero) != 0) {
Reid Spencer266e42b2006-12-23 06:05:41 +00005490 // Compute the Min, Max and RHS values based on the known bits. For the
5491 // EQ and NE we use unsigned values.
Zhou Sheng150f3bb2007-04-01 17:13:37 +00005492 APInt Min(BitWidth, 0), Max(BitWidth, 0);
5493 const APInt& RHSVal = CI->getValue();
Reid Spencer266e42b2006-12-23 06:05:41 +00005494 if (ICmpInst::isSignedPredicate(I.getPredicate())) {
Reid Spencerc3e3b8a2007-03-22 20:36:03 +00005495 ComputeSignedMinMaxValuesFromKnownBits(Ty, KnownZero, KnownOne, Min,
5496 Max);
Reid Spencer266e42b2006-12-23 06:05:41 +00005497 } else {
Reid Spencerc3e3b8a2007-03-22 20:36:03 +00005498 ComputeUnsignedMinMaxValuesFromKnownBits(Ty, KnownZero, KnownOne, Min,
5499 Max);
Reid Spencer266e42b2006-12-23 06:05:41 +00005500 }
5501 switch (I.getPredicate()) { // LE/GE have been folded already.
5502 default: assert(0 && "Unknown icmp opcode!");
5503 case ICmpInst::ICMP_EQ:
Reid Spencerc3e3b8a2007-03-22 20:36:03 +00005504 if (Max.ult(RHSVal) || Min.ugt(RHSVal))
Zhou Sheng75b871f2007-01-11 12:24:14 +00005505 return ReplaceInstUsesWith(I, ConstantInt::getFalse());
Reid Spencer266e42b2006-12-23 06:05:41 +00005506 break;
5507 case ICmpInst::ICMP_NE:
Reid Spencerc3e3b8a2007-03-22 20:36:03 +00005508 if (Max.ult(RHSVal) || Min.ugt(RHSVal))
Zhou Sheng75b871f2007-01-11 12:24:14 +00005509 return ReplaceInstUsesWith(I, ConstantInt::getTrue());
Reid Spencer266e42b2006-12-23 06:05:41 +00005510 break;
5511 case ICmpInst::ICMP_ULT:
Reid Spencerc3e3b8a2007-03-22 20:36:03 +00005512 if (Max.ult(RHSVal))
Zhou Sheng75b871f2007-01-11 12:24:14 +00005513 return ReplaceInstUsesWith(I, ConstantInt::getTrue());
Chris Lattner467b69c2007-04-09 23:52:13 +00005514 if (Min.uge(RHSVal))
Zhou Sheng75b871f2007-01-11 12:24:14 +00005515 return ReplaceInstUsesWith(I, ConstantInt::getFalse());
Reid Spencer266e42b2006-12-23 06:05:41 +00005516 break;
5517 case ICmpInst::ICMP_UGT:
Reid Spencerc3e3b8a2007-03-22 20:36:03 +00005518 if (Min.ugt(RHSVal))
Zhou Sheng75b871f2007-01-11 12:24:14 +00005519 return ReplaceInstUsesWith(I, ConstantInt::getTrue());
Chris Lattner467b69c2007-04-09 23:52:13 +00005520 if (Max.ule(RHSVal))
Zhou Sheng75b871f2007-01-11 12:24:14 +00005521 return ReplaceInstUsesWith(I, ConstantInt::getFalse());
Reid Spencer266e42b2006-12-23 06:05:41 +00005522 break;
5523 case ICmpInst::ICMP_SLT:
Reid Spencerc3e3b8a2007-03-22 20:36:03 +00005524 if (Max.slt(RHSVal))
Zhou Sheng75b871f2007-01-11 12:24:14 +00005525 return ReplaceInstUsesWith(I, ConstantInt::getTrue());
Reid Spencerc3e3b8a2007-03-22 20:36:03 +00005526 if (Min.sgt(RHSVal))
Zhou Sheng75b871f2007-01-11 12:24:14 +00005527 return ReplaceInstUsesWith(I, ConstantInt::getFalse());
Reid Spencer266e42b2006-12-23 06:05:41 +00005528 break;
5529 case ICmpInst::ICMP_SGT:
Reid Spencerc3e3b8a2007-03-22 20:36:03 +00005530 if (Min.sgt(RHSVal))
Zhou Sheng75b871f2007-01-11 12:24:14 +00005531 return ReplaceInstUsesWith(I, ConstantInt::getTrue());
Chris Lattner467b69c2007-04-09 23:52:13 +00005532 if (Max.sle(RHSVal))
Zhou Sheng75b871f2007-01-11 12:24:14 +00005533 return ReplaceInstUsesWith(I, ConstantInt::getFalse());
Reid Spencer266e42b2006-12-23 06:05:41 +00005534 break;
Chris Lattneree0f2802006-02-12 02:07:56 +00005535 }
5536 }
5537
Reid Spencer266e42b2006-12-23 06:05:41 +00005538 // Since the RHS is a ConstantInt (CI), if the left hand side is an
Reid Spencer7e80b0b2006-10-26 06:15:43 +00005539 // instruction, see if that instruction also has constants so that the
Reid Spencer266e42b2006-12-23 06:05:41 +00005540 // instruction can be folded into the icmp
Chris Lattnere1e10e12004-05-25 06:32:08 +00005541 if (Instruction *LHSI = dyn_cast<Instruction>(Op0))
Chris Lattnera74deaf2007-04-03 17:43:25 +00005542 if (Instruction *Res = visitICmpInstWithInstAndIntCst(I, LHSI, CI))
5543 return Res;
Chris Lattnerf4cdbf32002-05-06 16:14:14 +00005544 }
5545
Chris Lattnera74deaf2007-04-03 17:43:25 +00005546 // Handle icmp with constant (but not simple integer constant) RHS
Chris Lattner77c32c32005-04-23 15:31:55 +00005547 if (Constant *RHSC = dyn_cast<Constant>(Op1)) {
5548 if (Instruction *LHSI = dyn_cast<Instruction>(Op0))
5549 switch (LHSI->getOpcode()) {
Chris Lattnera816eee2005-05-01 04:42:15 +00005550 case Instruction::GetElementPtr:
5551 if (RHSC->isNullValue()) {
Reid Spencer266e42b2006-12-23 06:05:41 +00005552 // icmp pred GEP (P, int 0, int 0, int 0), null -> icmp pred P, null
Chris Lattnera816eee2005-05-01 04:42:15 +00005553 bool isAllZeros = true;
5554 for (unsigned i = 1, e = LHSI->getNumOperands(); i != e; ++i)
5555 if (!isa<Constant>(LHSI->getOperand(i)) ||
5556 !cast<Constant>(LHSI->getOperand(i))->isNullValue()) {
5557 isAllZeros = false;
5558 break;
5559 }
5560 if (isAllZeros)
Reid Spencer266e42b2006-12-23 06:05:41 +00005561 return new ICmpInst(I.getPredicate(), LHSI->getOperand(0),
Chris Lattnera816eee2005-05-01 04:42:15 +00005562 Constant::getNullValue(LHSI->getOperand(0)->getType()));
5563 }
5564 break;
5565
Chris Lattner77c32c32005-04-23 15:31:55 +00005566 case Instruction::PHI:
5567 if (Instruction *NV = FoldOpIntoPhi(I))
5568 return NV;
5569 break;
Chris Lattner3dbe65f2007-04-06 18:57:34 +00005570 case Instruction::Select: {
Chris Lattner77c32c32005-04-23 15:31:55 +00005571 // If either operand of the select is a constant, we can fold the
5572 // comparison into the select arms, which will cause one to be
5573 // constant folded and the select turned into a bitwise or.
5574 Value *Op1 = 0, *Op2 = 0;
5575 if (LHSI->hasOneUse()) {
5576 if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(1))) {
5577 // Fold the known value into the constant operand.
Reid Spencer266e42b2006-12-23 06:05:41 +00005578 Op1 = ConstantExpr::getICmp(I.getPredicate(), C, RHSC);
5579 // Insert a new ICmp of the other select operand.
5580 Op2 = InsertNewInstBefore(new ICmpInst(I.getPredicate(),
5581 LHSI->getOperand(2), RHSC,
5582 I.getName()), I);
Chris Lattner77c32c32005-04-23 15:31:55 +00005583 } else if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(2))) {
5584 // Fold the known value into the constant operand.
Reid Spencer266e42b2006-12-23 06:05:41 +00005585 Op2 = ConstantExpr::getICmp(I.getPredicate(), C, RHSC);
5586 // Insert a new ICmp of the other select operand.
5587 Op1 = InsertNewInstBefore(new ICmpInst(I.getPredicate(),
5588 LHSI->getOperand(1), RHSC,
5589 I.getName()), I);
Chris Lattner77c32c32005-04-23 15:31:55 +00005590 }
5591 }
Jeff Cohen82639852005-04-23 21:38:35 +00005592
Chris Lattner77c32c32005-04-23 15:31:55 +00005593 if (Op1)
Gabor Greife9ecc682008-04-06 20:25:17 +00005594 return SelectInst::Create(LHSI->getOperand(0), Op1, Op2);
Chris Lattner77c32c32005-04-23 15:31:55 +00005595 break;
5596 }
Chris Lattner3dbe65f2007-04-06 18:57:34 +00005597 case Instruction::Malloc:
5598 // If we have (malloc != null), and if the malloc has a single use, we
5599 // can assume it is successful and remove the malloc.
5600 if (LHSI->hasOneUse() && isa<ConstantPointerNull>(RHSC)) {
5601 AddToWorkList(LHSI);
5602 return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty,
5603 !isTrueWhenEqual(I)));
5604 }
5605 break;
5606 }
Chris Lattner77c32c32005-04-23 15:31:55 +00005607 }
5608
Reid Spencer266e42b2006-12-23 06:05:41 +00005609 // If we can optimize a 'icmp GEP, P' or 'icmp P, GEP', do so now.
Chris Lattner0798af32005-01-13 20:14:25 +00005610 if (User *GEP = dyn_castGetElementPtr(Op0))
Reid Spencer266e42b2006-12-23 06:05:41 +00005611 if (Instruction *NI = FoldGEPICmp(GEP, Op1, I.getPredicate(), I))
Chris Lattner0798af32005-01-13 20:14:25 +00005612 return NI;
5613 if (User *GEP = dyn_castGetElementPtr(Op1))
Reid Spencer266e42b2006-12-23 06:05:41 +00005614 if (Instruction *NI = FoldGEPICmp(GEP, Op0,
5615 ICmpInst::getSwappedPredicate(I.getPredicate()), I))
Chris Lattner0798af32005-01-13 20:14:25 +00005616 return NI;
5617
Reid Spencer266e42b2006-12-23 06:05:41 +00005618 // Test to see if the operands of the icmp are casted versions of other
Chris Lattner64d87b02007-01-06 01:45:59 +00005619 // values. If the ptr->ptr cast can be stripped off both arguments, we do so
5620 // now.
5621 if (BitCastInst *CI = dyn_cast<BitCastInst>(Op0)) {
5622 if (isa<PointerType>(Op0->getType()) &&
5623 (isa<Constant>(Op1) || isa<BitCastInst>(Op1))) {
Chris Lattner16930792003-11-03 04:25:02 +00005624 // We keep moving the cast from the left operand over to the right
5625 // operand, where it can often be eliminated completely.
Chris Lattner64d87b02007-01-06 01:45:59 +00005626 Op0 = CI->getOperand(0);
Misha Brukmanb1c93172005-04-21 23:48:37 +00005627
Chris Lattner64d87b02007-01-06 01:45:59 +00005628 // If operand #1 is a bitcast instruction, it must also be a ptr->ptr cast
5629 // so eliminate it as well.
5630 if (BitCastInst *CI2 = dyn_cast<BitCastInst>(Op1))
5631 Op1 = CI2->getOperand(0);
Misha Brukmanb1c93172005-04-21 23:48:37 +00005632
Chris Lattner16930792003-11-03 04:25:02 +00005633 // If Op1 is a constant, we can fold the cast into the constant.
Anton Korobeynikov1bfd1212008-02-20 11:26:25 +00005634 if (Op0->getType() != Op1->getType()) {
Chris Lattner16930792003-11-03 04:25:02 +00005635 if (Constant *Op1C = dyn_cast<Constant>(Op1)) {
Reid Spencerbb65ebf2006-12-12 23:36:14 +00005636 Op1 = ConstantExpr::getBitCast(Op1C, Op0->getType());
Chris Lattner16930792003-11-03 04:25:02 +00005637 } else {
Reid Spencer266e42b2006-12-23 06:05:41 +00005638 // Otherwise, cast the RHS right before the icmp
Chris Lattner5a866122008-01-13 22:23:22 +00005639 Op1 = InsertBitCastBefore(Op1, Op0->getType(), I);
Chris Lattner16930792003-11-03 04:25:02 +00005640 }
Anton Korobeynikov1bfd1212008-02-20 11:26:25 +00005641 }
Reid Spencer266e42b2006-12-23 06:05:41 +00005642 return new ICmpInst(I.getPredicate(), Op0, Op1);
Chris Lattner16930792003-11-03 04:25:02 +00005643 }
Chris Lattner64d87b02007-01-06 01:45:59 +00005644 }
5645
5646 if (isa<CastInst>(Op0)) {
Reid Spencer266e42b2006-12-23 06:05:41 +00005647 // Handle the special case of: icmp (cast bool to X), <cst>
Chris Lattner6444c372003-11-03 05:17:03 +00005648 // This comes up when you have code like
5649 // int X = A < B;
5650 // if (X) ...
5651 // For generality, we handle any zero-extension of any operand comparison
Chris Lattnerd1f46d32005-04-24 06:59:08 +00005652 // with a constant or another cast from the same type.
5653 if (isa<ConstantInt>(Op1) || isa<CastInst>(Op1))
Reid Spencer266e42b2006-12-23 06:05:41 +00005654 if (Instruction *R = visitICmpInstWithCastAndCast(I))
Chris Lattnerd1f46d32005-04-24 06:59:08 +00005655 return R;
Chris Lattner6444c372003-11-03 05:17:03 +00005656 }
Chris Lattnerf5c8a0b2006-02-27 01:44:11 +00005657
Chris Lattnerb3f24c92006-09-18 04:22:48 +00005658 if (I.isEquality()) {
Chris Lattner17c7c032007-01-05 03:04:57 +00005659 Value *A, *B, *C, *D;
5660 if (match(Op0, m_Xor(m_Value(A), m_Value(B)))) {
5661 if (A == Op1 || B == Op1) { // (A^B) == A -> B == 0
5662 Value *OtherVal = A == Op1 ? B : A;
5663 return new ICmpInst(I.getPredicate(), OtherVal,
5664 Constant::getNullValue(A->getType()));
5665 }
5666
5667 if (match(Op1, m_Xor(m_Value(C), m_Value(D)))) {
5668 // A^c1 == C^c2 --> A == C^(c1^c2)
5669 if (ConstantInt *C1 = dyn_cast<ConstantInt>(B))
5670 if (ConstantInt *C2 = dyn_cast<ConstantInt>(D))
5671 if (Op1->hasOneUse()) {
Zhou Sheng9bc8ab12007-04-02 13:45:30 +00005672 Constant *NC = ConstantInt::get(C1->getValue() ^ C2->getValue());
Chris Lattner17c7c032007-01-05 03:04:57 +00005673 Instruction *Xor = BinaryOperator::createXor(C, NC, "tmp");
5674 return new ICmpInst(I.getPredicate(), A,
5675 InsertNewInstBefore(Xor, I));
5676 }
5677
5678 // A^B == A^D -> B == D
5679 if (A == C) return new ICmpInst(I.getPredicate(), B, D);
5680 if (A == D) return new ICmpInst(I.getPredicate(), B, C);
5681 if (B == C) return new ICmpInst(I.getPredicate(), A, D);
5682 if (B == D) return new ICmpInst(I.getPredicate(), A, C);
5683 }
5684 }
5685
5686 if (match(Op1, m_Xor(m_Value(A), m_Value(B))) &&
5687 (A == Op0 || B == Op0)) {
Chris Lattnerf5c8a0b2006-02-27 01:44:11 +00005688 // A == (A^B) -> B == 0
5689 Value *OtherVal = A == Op0 ? B : A;
Reid Spencer266e42b2006-12-23 06:05:41 +00005690 return new ICmpInst(I.getPredicate(), OtherVal,
5691 Constant::getNullValue(A->getType()));
Chris Lattner17c7c032007-01-05 03:04:57 +00005692 }
5693 if (match(Op0, m_Sub(m_Value(A), m_Value(B))) && A == Op1) {
Chris Lattnerf5c8a0b2006-02-27 01:44:11 +00005694 // (A-B) == A -> B == 0
Reid Spencer266e42b2006-12-23 06:05:41 +00005695 return new ICmpInst(I.getPredicate(), B,
5696 Constant::getNullValue(B->getType()));
Chris Lattner17c7c032007-01-05 03:04:57 +00005697 }
5698 if (match(Op1, m_Sub(m_Value(A), m_Value(B))) && A == Op0) {
Chris Lattnerf5c8a0b2006-02-27 01:44:11 +00005699 // A == (A-B) -> B == 0
Reid Spencer266e42b2006-12-23 06:05:41 +00005700 return new ICmpInst(I.getPredicate(), B,
5701 Constant::getNullValue(B->getType()));
Chris Lattnerf5c8a0b2006-02-27 01:44:11 +00005702 }
Chris Lattnerd12a4bf2006-11-14 06:06:06 +00005703
Chris Lattnerd12a4bf2006-11-14 06:06:06 +00005704 // (X&Z) == (Y&Z) -> (X^Y) & Z == 0
5705 if (Op0->hasOneUse() && Op1->hasOneUse() &&
5706 match(Op0, m_And(m_Value(A), m_Value(B))) &&
5707 match(Op1, m_And(m_Value(C), m_Value(D)))) {
5708 Value *X = 0, *Y = 0, *Z = 0;
5709
5710 if (A == C) {
5711 X = B; Y = D; Z = A;
5712 } else if (A == D) {
5713 X = B; Y = C; Z = A;
5714 } else if (B == C) {
5715 X = A; Y = D; Z = B;
5716 } else if (B == D) {
5717 X = A; Y = C; Z = B;
5718 }
5719
5720 if (X) { // Build (X^Y) & Z
5721 Op1 = InsertNewInstBefore(BinaryOperator::createXor(X, Y, "tmp"), I);
5722 Op1 = InsertNewInstBefore(BinaryOperator::createAnd(Op1, Z, "tmp"), I);
5723 I.setOperand(0, Op1);
5724 I.setOperand(1, Constant::getNullValue(Op1->getType()));
5725 return &I;
5726 }
5727 }
Chris Lattnerf5c8a0b2006-02-27 01:44:11 +00005728 }
Chris Lattner113f4f42002-06-25 16:13:24 +00005729 return Changed ? &I : 0;
Chris Lattnerf4cdbf32002-05-06 16:14:14 +00005730}
5731
Chris Lattner3bbec592007-06-20 23:46:26 +00005732
5733/// FoldICmpDivCst - Fold "icmp pred, ([su]div X, DivRHS), CmpRHS" where DivRHS
5734/// and CmpRHS are both known to be integer constants.
5735Instruction *InstCombiner::FoldICmpDivCst(ICmpInst &ICI, BinaryOperator *DivI,
5736 ConstantInt *DivRHS) {
5737 ConstantInt *CmpRHS = cast<ConstantInt>(ICI.getOperand(1));
5738 const APInt &CmpRHSV = CmpRHS->getValue();
5739
5740 // FIXME: If the operand types don't match the type of the divide
5741 // then don't attempt this transform. The code below doesn't have the
5742 // logic to deal with a signed divide and an unsigned compare (and
5743 // vice versa). This is because (x /s C1) <s C2 produces different
5744 // results than (x /s C1) <u C2 or (x /u C1) <s C2 or even
5745 // (x /u C1) <u C2. Simply casting the operands and result won't
5746 // work. :( The if statement below tests that condition and bails
5747 // if it finds it.
5748 bool DivIsSigned = DivI->getOpcode() == Instruction::SDiv;
5749 if (!ICI.isEquality() && DivIsSigned != ICI.isSignedPredicate())
5750 return 0;
5751 if (DivRHS->isZero())
Chris Lattnerfb032b12007-06-21 18:11:19 +00005752 return 0; // The ProdOV computation fails on divide by zero.
Chris Lattner3bbec592007-06-20 23:46:26 +00005753
5754 // Compute Prod = CI * DivRHS. We are essentially solving an equation
5755 // of form X/C1=C2. We solve for X by multiplying C1 (DivRHS) and
5756 // C2 (CI). By solving for X we can turn this into a range check
5757 // instead of computing a divide.
5758 ConstantInt *Prod = Multiply(CmpRHS, DivRHS);
5759
5760 // Determine if the product overflows by seeing if the product is
5761 // not equal to the divide. Make sure we do the same kind of divide
5762 // as in the LHS instruction that we're folding.
5763 bool ProdOV = (DivIsSigned ? ConstantExpr::getSDiv(Prod, DivRHS) :
5764 ConstantExpr::getUDiv(Prod, DivRHS)) != CmpRHS;
5765
5766 // Get the ICmp opcode
Chris Lattnerfb032b12007-06-21 18:11:19 +00005767 ICmpInst::Predicate Pred = ICI.getPredicate();
Chris Lattner3bbec592007-06-20 23:46:26 +00005768
Chris Lattnerfb032b12007-06-21 18:11:19 +00005769 // Figure out the interval that is being checked. For example, a comparison
5770 // like "X /u 5 == 0" is really checking that X is in the interval [0, 5).
5771 // Compute this interval based on the constants involved and the signedness of
5772 // the compare/divide. This computes a half-open interval, keeping track of
5773 // whether either value in the interval overflows. After analysis each
5774 // overflow variable is set to 0 if it's corresponding bound variable is valid
5775 // -1 if overflowed off the bottom end, or +1 if overflowed off the top end.
5776 int LoOverflow = 0, HiOverflow = 0;
5777 ConstantInt *LoBound = 0, *HiBound = 0;
5778
5779
Chris Lattner3bbec592007-06-20 23:46:26 +00005780 if (!DivIsSigned) { // udiv
Chris Lattnerfb032b12007-06-21 18:11:19 +00005781 // e.g. X/5 op 3 --> [15, 20)
Chris Lattner3bbec592007-06-20 23:46:26 +00005782 LoBound = Prod;
Chris Lattnerfb032b12007-06-21 18:11:19 +00005783 HiOverflow = LoOverflow = ProdOV;
5784 if (!HiOverflow)
5785 HiOverflow = AddWithOverflow(HiBound, LoBound, DivRHS, false);
Dan Gohman1ee8dc92008-02-13 22:09:18 +00005786 } else if (DivRHS->getValue().isStrictlyPositive()) { // Divisor is > 0.
Chris Lattner3bbec592007-06-20 23:46:26 +00005787 if (CmpRHSV == 0) { // (X / pos) op 0
Chris Lattnerfb032b12007-06-21 18:11:19 +00005788 // Can't overflow. e.g. X/2 op 0 --> [-1, 2)
Chris Lattner3bbec592007-06-20 23:46:26 +00005789 LoBound = cast<ConstantInt>(ConstantExpr::getNeg(SubOne(DivRHS)));
5790 HiBound = DivRHS;
Dan Gohman1ee8dc92008-02-13 22:09:18 +00005791 } else if (CmpRHSV.isStrictlyPositive()) { // (X / pos) op pos
Chris Lattnerfb032b12007-06-21 18:11:19 +00005792 LoBound = Prod; // e.g. X/5 op 3 --> [15, 20)
5793 HiOverflow = LoOverflow = ProdOV;
5794 if (!HiOverflow)
5795 HiOverflow = AddWithOverflow(HiBound, Prod, DivRHS, true);
Chris Lattner3bbec592007-06-20 23:46:26 +00005796 } else { // (X / pos) op neg
Chris Lattnerfb032b12007-06-21 18:11:19 +00005797 // e.g. X/5 op -3 --> [-15-4, -15+1) --> [-19, -14)
Chris Lattner3bbec592007-06-20 23:46:26 +00005798 Constant *DivRHSH = ConstantExpr::getNeg(SubOne(DivRHS));
5799 LoOverflow = AddWithOverflow(LoBound, Prod,
Chris Lattnerfb032b12007-06-21 18:11:19 +00005800 cast<ConstantInt>(DivRHSH), true) ? -1 : 0;
Chris Lattner3bbec592007-06-20 23:46:26 +00005801 HiBound = AddOne(Prod);
Chris Lattnerfb032b12007-06-21 18:11:19 +00005802 HiOverflow = ProdOV ? -1 : 0;
Chris Lattner3bbec592007-06-20 23:46:26 +00005803 }
Dan Gohman1ee8dc92008-02-13 22:09:18 +00005804 } else if (DivRHS->getValue().isNegative()) { // Divisor is < 0.
Chris Lattner3bbec592007-06-20 23:46:26 +00005805 if (CmpRHSV == 0) { // (X / neg) op 0
Chris Lattnerfb032b12007-06-21 18:11:19 +00005806 // e.g. X/-5 op 0 --> [-4, 5)
Chris Lattner3bbec592007-06-20 23:46:26 +00005807 LoBound = AddOne(DivRHS);
5808 HiBound = cast<ConstantInt>(ConstantExpr::getNeg(DivRHS));
Chris Lattnerfb032b12007-06-21 18:11:19 +00005809 if (HiBound == DivRHS) { // -INTMIN = INTMIN
5810 HiOverflow = 1; // [INTMIN+1, overflow)
5811 HiBound = 0; // e.g. X/INTMIN = 0 --> X > INTMIN
5812 }
Dan Gohman1ee8dc92008-02-13 22:09:18 +00005813 } else if (CmpRHSV.isStrictlyPositive()) { // (X / neg) op pos
Chris Lattnerfb032b12007-06-21 18:11:19 +00005814 // e.g. X/-5 op 3 --> [-19, -14)
5815 HiOverflow = LoOverflow = ProdOV ? -1 : 0;
Chris Lattner3bbec592007-06-20 23:46:26 +00005816 if (!LoOverflow)
Chris Lattnerfb032b12007-06-21 18:11:19 +00005817 LoOverflow = AddWithOverflow(LoBound, Prod, AddOne(DivRHS), true) ?-1:0;
Chris Lattner3bbec592007-06-20 23:46:26 +00005818 HiBound = AddOne(Prod);
5819 } else { // (X / neg) op neg
Chris Lattnerfb032b12007-06-21 18:11:19 +00005820 // e.g. X/-5 op -3 --> [15, 20)
Chris Lattner3bbec592007-06-20 23:46:26 +00005821 LoBound = Prod;
Chris Lattnerfb032b12007-06-21 18:11:19 +00005822 LoOverflow = HiOverflow = ProdOV ? 1 : 0;
Chris Lattner3bbec592007-06-20 23:46:26 +00005823 HiBound = Subtract(Prod, DivRHS);
5824 }
5825
Chris Lattnerfb032b12007-06-21 18:11:19 +00005826 // Dividing by a negative swaps the condition. LT <-> GT
5827 Pred = ICmpInst::getSwappedPredicate(Pred);
Chris Lattner3bbec592007-06-20 23:46:26 +00005828 }
5829
5830 Value *X = DivI->getOperand(0);
Chris Lattnerfb032b12007-06-21 18:11:19 +00005831 switch (Pred) {
Chris Lattner3bbec592007-06-20 23:46:26 +00005832 default: assert(0 && "Unhandled icmp opcode!");
5833 case ICmpInst::ICMP_EQ:
5834 if (LoOverflow && HiOverflow)
5835 return ReplaceInstUsesWith(ICI, ConstantInt::getFalse());
5836 else if (HiOverflow)
Chris Lattnerfb032b12007-06-21 18:11:19 +00005837 return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SGE :
Chris Lattner3bbec592007-06-20 23:46:26 +00005838 ICmpInst::ICMP_UGE, X, LoBound);
5839 else if (LoOverflow)
5840 return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SLT :
5841 ICmpInst::ICMP_ULT, X, HiBound);
5842 else
Chris Lattnerfb032b12007-06-21 18:11:19 +00005843 return InsertRangeTest(X, LoBound, HiBound, DivIsSigned, true, ICI);
Chris Lattner3bbec592007-06-20 23:46:26 +00005844 case ICmpInst::ICMP_NE:
5845 if (LoOverflow && HiOverflow)
5846 return ReplaceInstUsesWith(ICI, ConstantInt::getTrue());
5847 else if (HiOverflow)
Chris Lattnerfb032b12007-06-21 18:11:19 +00005848 return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SLT :
Chris Lattner3bbec592007-06-20 23:46:26 +00005849 ICmpInst::ICMP_ULT, X, LoBound);
5850 else if (LoOverflow)
5851 return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SGE :
5852 ICmpInst::ICMP_UGE, X, HiBound);
5853 else
Chris Lattnerfb032b12007-06-21 18:11:19 +00005854 return InsertRangeTest(X, LoBound, HiBound, DivIsSigned, false, ICI);
Chris Lattner3bbec592007-06-20 23:46:26 +00005855 case ICmpInst::ICMP_ULT:
5856 case ICmpInst::ICMP_SLT:
Chris Lattnerfb032b12007-06-21 18:11:19 +00005857 if (LoOverflow == +1) // Low bound is greater than input range.
5858 return ReplaceInstUsesWith(ICI, ConstantInt::getTrue());
5859 if (LoOverflow == -1) // Low bound is less than input range.
Chris Lattner3bbec592007-06-20 23:46:26 +00005860 return ReplaceInstUsesWith(ICI, ConstantInt::getFalse());
Chris Lattnerfb032b12007-06-21 18:11:19 +00005861 return new ICmpInst(Pred, X, LoBound);
Chris Lattner3bbec592007-06-20 23:46:26 +00005862 case ICmpInst::ICMP_UGT:
5863 case ICmpInst::ICMP_SGT:
Chris Lattnerfb032b12007-06-21 18:11:19 +00005864 if (HiOverflow == +1) // High bound greater than input range.
Chris Lattner3bbec592007-06-20 23:46:26 +00005865 return ReplaceInstUsesWith(ICI, ConstantInt::getFalse());
Chris Lattnerfb032b12007-06-21 18:11:19 +00005866 else if (HiOverflow == -1) // High bound less than input range.
5867 return ReplaceInstUsesWith(ICI, ConstantInt::getTrue());
5868 if (Pred == ICmpInst::ICMP_UGT)
Chris Lattner3bbec592007-06-20 23:46:26 +00005869 return new ICmpInst(ICmpInst::ICMP_UGE, X, HiBound);
5870 else
5871 return new ICmpInst(ICmpInst::ICMP_SGE, X, HiBound);
5872 }
5873}
5874
5875
Chris Lattnera74deaf2007-04-03 17:43:25 +00005876/// visitICmpInstWithInstAndIntCst - Handle "icmp (instr, intcst)".
5877///
5878Instruction *InstCombiner::visitICmpInstWithInstAndIntCst(ICmpInst &ICI,
5879 Instruction *LHSI,
5880 ConstantInt *RHS) {
5881 const APInt &RHSV = RHS->getValue();
5882
5883 switch (LHSI->getOpcode()) {
Duncan Sandsf01a47c2007-04-04 06:42:45 +00005884 case Instruction::Xor: // (icmp pred (xor X, XorCST), CI)
Chris Lattnera74deaf2007-04-03 17:43:25 +00005885 if (ConstantInt *XorCST = dyn_cast<ConstantInt>(LHSI->getOperand(1))) {
5886 // If this is a comparison that tests the signbit (X < 0) or (x > -1),
5887 // fold the xor.
Anton Korobeynikov1bfd1212008-02-20 11:26:25 +00005888 if ((ICI.getPredicate() == ICmpInst::ICMP_SLT && RHSV == 0) ||
5889 (ICI.getPredicate() == ICmpInst::ICMP_SGT && RHSV.isAllOnesValue())) {
Chris Lattnera74deaf2007-04-03 17:43:25 +00005890 Value *CompareVal = LHSI->getOperand(0);
5891
5892 // If the sign bit of the XorCST is not set, there is no change to
5893 // the operation, just stop using the Xor.
5894 if (!XorCST->getValue().isNegative()) {
5895 ICI.setOperand(0, CompareVal);
5896 AddToWorkList(LHSI);
5897 return &ICI;
5898 }
5899
5900 // Was the old condition true if the operand is positive?
5901 bool isTrueIfPositive = ICI.getPredicate() == ICmpInst::ICMP_SGT;
5902
5903 // If so, the new one isn't.
5904 isTrueIfPositive ^= true;
5905
5906 if (isTrueIfPositive)
5907 return new ICmpInst(ICmpInst::ICMP_SGT, CompareVal, SubOne(RHS));
5908 else
5909 return new ICmpInst(ICmpInst::ICMP_SLT, CompareVal, AddOne(RHS));
5910 }
5911 }
5912 break;
5913 case Instruction::And: // (icmp pred (and X, AndCST), RHS)
5914 if (LHSI->hasOneUse() && isa<ConstantInt>(LHSI->getOperand(1)) &&
5915 LHSI->getOperand(0)->hasOneUse()) {
5916 ConstantInt *AndCST = cast<ConstantInt>(LHSI->getOperand(1));
5917
5918 // If the LHS is an AND of a truncating cast, we can widen the
5919 // and/compare to be the input width without changing the value
5920 // produced, eliminating a cast.
5921 if (TruncInst *Cast = dyn_cast<TruncInst>(LHSI->getOperand(0))) {
5922 // We can do this transformation if either the AND constant does not
5923 // have its sign bit set or if it is an equality comparison.
5924 // Extending a relational comparison when we're checking the sign
5925 // bit would not work.
5926 if (Cast->hasOneUse() &&
Anton Korobeynikov18991d72008-02-20 12:07:57 +00005927 (ICI.isEquality() ||
5928 (AndCST->getValue().isNonNegative() && RHSV.isNonNegative()))) {
Chris Lattnera74deaf2007-04-03 17:43:25 +00005929 uint32_t BitWidth =
5930 cast<IntegerType>(Cast->getOperand(0)->getType())->getBitWidth();
5931 APInt NewCST = AndCST->getValue();
5932 NewCST.zext(BitWidth);
5933 APInt NewCI = RHSV;
5934 NewCI.zext(BitWidth);
5935 Instruction *NewAnd =
5936 BinaryOperator::createAnd(Cast->getOperand(0),
5937 ConstantInt::get(NewCST),LHSI->getName());
5938 InsertNewInstBefore(NewAnd, ICI);
5939 return new ICmpInst(ICI.getPredicate(), NewAnd,
5940 ConstantInt::get(NewCI));
5941 }
5942 }
5943
5944 // If this is: (X >> C1) & C2 != C3 (where any shift and any compare
5945 // could exist), turn it into (X & (C2 << C1)) != (C3 << C1). This
5946 // happens a LOT in code produced by the C front-end, for bitfield
5947 // access.
5948 BinaryOperator *Shift = dyn_cast<BinaryOperator>(LHSI->getOperand(0));
5949 if (Shift && !Shift->isShift())
5950 Shift = 0;
5951
5952 ConstantInt *ShAmt;
5953 ShAmt = Shift ? dyn_cast<ConstantInt>(Shift->getOperand(1)) : 0;
5954 const Type *Ty = Shift ? Shift->getType() : 0; // Type of the shift.
5955 const Type *AndTy = AndCST->getType(); // Type of the and.
5956
5957 // We can fold this as long as we can't shift unknown bits
5958 // into the mask. This can only happen with signed shift
5959 // rights, as they sign-extend.
5960 if (ShAmt) {
5961 bool CanFold = Shift->isLogicalShift();
5962 if (!CanFold) {
5963 // To test for the bad case of the signed shr, see if any
5964 // of the bits shifted in could be tested after the mask.
5965 uint32_t TyBits = Ty->getPrimitiveSizeInBits();
5966 int ShAmtVal = TyBits - ShAmt->getLimitedValue(TyBits);
5967
5968 uint32_t BitWidth = AndTy->getPrimitiveSizeInBits();
5969 if ((APInt::getHighBitsSet(BitWidth, BitWidth-ShAmtVal) &
5970 AndCST->getValue()) == 0)
5971 CanFold = true;
5972 }
5973
5974 if (CanFold) {
5975 Constant *NewCst;
5976 if (Shift->getOpcode() == Instruction::Shl)
5977 NewCst = ConstantExpr::getLShr(RHS, ShAmt);
5978 else
5979 NewCst = ConstantExpr::getShl(RHS, ShAmt);
5980
5981 // Check to see if we are shifting out any of the bits being
5982 // compared.
5983 if (ConstantExpr::get(Shift->getOpcode(), NewCst, ShAmt) != RHS) {
5984 // If we shifted bits out, the fold is not going to work out.
5985 // As a special case, check to see if this means that the
5986 // result is always true or false now.
5987 if (ICI.getPredicate() == ICmpInst::ICMP_EQ)
5988 return ReplaceInstUsesWith(ICI, ConstantInt::getFalse());
5989 if (ICI.getPredicate() == ICmpInst::ICMP_NE)
5990 return ReplaceInstUsesWith(ICI, ConstantInt::getTrue());
5991 } else {
5992 ICI.setOperand(1, NewCst);
5993 Constant *NewAndCST;
5994 if (Shift->getOpcode() == Instruction::Shl)
5995 NewAndCST = ConstantExpr::getLShr(AndCST, ShAmt);
5996 else
5997 NewAndCST = ConstantExpr::getShl(AndCST, ShAmt);
5998 LHSI->setOperand(1, NewAndCST);
5999 LHSI->setOperand(0, Shift->getOperand(0));
6000 AddToWorkList(Shift); // Shift is dead.
6001 AddUsesToWorkList(ICI);
6002 return &ICI;
6003 }
6004 }
6005 }
6006
6007 // Turn ((X >> Y) & C) == 0 into (X & (C << Y)) == 0. The later is
6008 // preferable because it allows the C<<Y expression to be hoisted out
6009 // of a loop if Y is invariant and X is not.
6010 if (Shift && Shift->hasOneUse() && RHSV == 0 &&
6011 ICI.isEquality() && !Shift->isArithmeticShift() &&
6012 isa<Instruction>(Shift->getOperand(0))) {
6013 // Compute C << Y.
6014 Value *NS;
6015 if (Shift->getOpcode() == Instruction::LShr) {
6016 NS = BinaryOperator::createShl(AndCST,
6017 Shift->getOperand(1), "tmp");
6018 } else {
6019 // Insert a logical shift.
6020 NS = BinaryOperator::createLShr(AndCST,
6021 Shift->getOperand(1), "tmp");
6022 }
6023 InsertNewInstBefore(cast<Instruction>(NS), ICI);
6024
6025 // Compute X & (C << Y).
6026 Instruction *NewAnd =
6027 BinaryOperator::createAnd(Shift->getOperand(0), NS, LHSI->getName());
6028 InsertNewInstBefore(NewAnd, ICI);
6029
6030 ICI.setOperand(0, NewAnd);
6031 return &ICI;
6032 }
6033 }
6034 break;
6035
Chris Lattner06205d52007-07-15 20:42:37 +00006036 case Instruction::Shl: { // (icmp pred (shl X, ShAmt), CI)
6037 ConstantInt *ShAmt = dyn_cast<ConstantInt>(LHSI->getOperand(1));
6038 if (!ShAmt) break;
6039
6040 uint32_t TypeBits = RHSV.getBitWidth();
6041
6042 // Check that the shift amount is in range. If not, don't perform
6043 // undefined shifts. When the shift is visited it will be
6044 // simplified.
6045 if (ShAmt->uge(TypeBits))
6046 break;
6047
6048 if (ICI.isEquality()) {
6049 // If we are comparing against bits always shifted out, the
6050 // comparison cannot succeed.
6051 Constant *Comp =
6052 ConstantExpr::getShl(ConstantExpr::getLShr(RHS, ShAmt), ShAmt);
6053 if (Comp != RHS) {// Comparing against a bit that we know is zero.
6054 bool IsICMP_NE = ICI.getPredicate() == ICmpInst::ICMP_NE;
6055 Constant *Cst = ConstantInt::get(Type::Int1Ty, IsICMP_NE);
6056 return ReplaceInstUsesWith(ICI, Cst);
6057 }
6058
6059 if (LHSI->hasOneUse()) {
6060 // Otherwise strength reduce the shift into an and.
6061 uint32_t ShAmtVal = (uint32_t)ShAmt->getLimitedValue(TypeBits);
6062 Constant *Mask =
6063 ConstantInt::get(APInt::getLowBitsSet(TypeBits, TypeBits-ShAmtVal));
Chris Lattnera74deaf2007-04-03 17:43:25 +00006064
Chris Lattner06205d52007-07-15 20:42:37 +00006065 Instruction *AndI =
6066 BinaryOperator::createAnd(LHSI->getOperand(0),
6067 Mask, LHSI->getName()+".mask");
6068 Value *And = InsertNewInstBefore(AndI, ICI);
6069 return new ICmpInst(ICI.getPredicate(), And,
6070 ConstantInt::get(RHSV.lshr(ShAmtVal)));
Chris Lattnera74deaf2007-04-03 17:43:25 +00006071 }
6072 }
Chris Lattner06205d52007-07-15 20:42:37 +00006073
6074 // Otherwise, if this is a comparison of the sign bit, simplify to and/test.
6075 bool TrueIfSigned = false;
6076 if (LHSI->hasOneUse() &&
6077 isSignBitCheck(ICI.getPredicate(), RHS, TrueIfSigned)) {
6078 // (X << 31) <s 0 --> (X&1) != 0
6079 Constant *Mask = ConstantInt::get(APInt(TypeBits, 1) <<
6080 (TypeBits-ShAmt->getZExtValue()-1));
6081 Instruction *AndI =
6082 BinaryOperator::createAnd(LHSI->getOperand(0),
6083 Mask, LHSI->getName()+".mask");
6084 Value *And = InsertNewInstBefore(AndI, ICI);
6085
6086 return new ICmpInst(TrueIfSigned ? ICmpInst::ICMP_NE : ICmpInst::ICMP_EQ,
6087 And, Constant::getNullValue(And->getType()));
6088 }
Chris Lattnera74deaf2007-04-03 17:43:25 +00006089 break;
Chris Lattner06205d52007-07-15 20:42:37 +00006090 }
Chris Lattnera74deaf2007-04-03 17:43:25 +00006091
6092 case Instruction::LShr: // (icmp pred (shr X, ShAmt), CI)
Chris Lattner06205d52007-07-15 20:42:37 +00006093 case Instruction::AShr: {
Chris Lattnerc44160c2008-03-21 05:19:58 +00006094 // Only handle equality comparisons of shift-by-constant.
Chris Lattner06205d52007-07-15 20:42:37 +00006095 ConstantInt *ShAmt = dyn_cast<ConstantInt>(LHSI->getOperand(1));
Chris Lattnerc44160c2008-03-21 05:19:58 +00006096 if (!ShAmt || !ICI.isEquality()) break;
Chris Lattner06205d52007-07-15 20:42:37 +00006097
Chris Lattnerc44160c2008-03-21 05:19:58 +00006098 // Check that the shift amount is in range. If not, don't perform
6099 // undefined shifts. When the shift is visited it will be
6100 // simplified.
6101 uint32_t TypeBits = RHSV.getBitWidth();
6102 if (ShAmt->uge(TypeBits))
6103 break;
6104
6105 uint32_t ShAmtVal = (uint32_t)ShAmt->getLimitedValue(TypeBits);
Chris Lattner06205d52007-07-15 20:42:37 +00006106
Chris Lattnerc44160c2008-03-21 05:19:58 +00006107 // If we are comparing against bits always shifted out, the
6108 // comparison cannot succeed.
6109 APInt Comp = RHSV << ShAmtVal;
6110 if (LHSI->getOpcode() == Instruction::LShr)
6111 Comp = Comp.lshr(ShAmtVal);
6112 else
6113 Comp = Comp.ashr(ShAmtVal);
6114
6115 if (Comp != RHSV) { // Comparing against a bit that we know is zero.
6116 bool IsICMP_NE = ICI.getPredicate() == ICmpInst::ICMP_NE;
6117 Constant *Cst = ConstantInt::get(Type::Int1Ty, IsICMP_NE);
6118 return ReplaceInstUsesWith(ICI, Cst);
6119 }
6120
6121 // Otherwise, check to see if the bits shifted out are known to be zero.
6122 // If so, we can compare against the unshifted value:
6123 // (X & 4) >> 1 == 2 --> (X & 4) == 4.
Evan Cheng1c89ca72008-04-23 00:38:06 +00006124 if (LHSI->hasOneUse() &&
6125 MaskedValueIsZero(LHSI->getOperand(0),
Chris Lattnerc44160c2008-03-21 05:19:58 +00006126 APInt::getLowBitsSet(Comp.getBitWidth(), ShAmtVal))) {
6127 return new ICmpInst(ICI.getPredicate(), LHSI->getOperand(0),
6128 ConstantExpr::getShl(RHS, ShAmt));
6129 }
Chris Lattner06205d52007-07-15 20:42:37 +00006130
Evan Cheng1c89ca72008-04-23 00:38:06 +00006131 if (LHSI->hasOneUse()) {
Chris Lattnerc44160c2008-03-21 05:19:58 +00006132 // Otherwise strength reduce the shift into an and.
6133 APInt Val(APInt::getHighBitsSet(TypeBits, TypeBits - ShAmtVal));
6134 Constant *Mask = ConstantInt::get(Val);
Chris Lattner06205d52007-07-15 20:42:37 +00006135
Chris Lattnerc44160c2008-03-21 05:19:58 +00006136 Instruction *AndI =
6137 BinaryOperator::createAnd(LHSI->getOperand(0),
6138 Mask, LHSI->getName()+".mask");
6139 Value *And = InsertNewInstBefore(AndI, ICI);
6140 return new ICmpInst(ICI.getPredicate(), And,
6141 ConstantExpr::getShl(RHS, ShAmt));
Chris Lattnera74deaf2007-04-03 17:43:25 +00006142 }
6143 break;
Chris Lattner06205d52007-07-15 20:42:37 +00006144 }
Chris Lattnera74deaf2007-04-03 17:43:25 +00006145
6146 case Instruction::SDiv:
6147 case Instruction::UDiv:
6148 // Fold: icmp pred ([us]div X, C1), C2 -> range test
6149 // Fold this div into the comparison, producing a range check.
6150 // Determine, based on the divide type, what the range is being
6151 // checked. If there is an overflow on the low or high side, remember
6152 // it, otherwise compute the range [low, hi) bounding the new value.
6153 // See: InsertRangeTest above for the kinds of replacements possible.
Chris Lattner3bbec592007-06-20 23:46:26 +00006154 if (ConstantInt *DivRHS = dyn_cast<ConstantInt>(LHSI->getOperand(1)))
6155 if (Instruction *R = FoldICmpDivCst(ICI, cast<BinaryOperator>(LHSI),
6156 DivRHS))
6157 return R;
Chris Lattnera74deaf2007-04-03 17:43:25 +00006158 break;
Nick Lewycky3b592142008-02-03 16:33:09 +00006159
6160 case Instruction::Add:
6161 // Fold: icmp pred (add, X, C1), C2
6162
6163 if (!ICI.isEquality()) {
6164 ConstantInt *LHSC = dyn_cast<ConstantInt>(LHSI->getOperand(1));
6165 if (!LHSC) break;
6166 const APInt &LHSV = LHSC->getValue();
6167
6168 ConstantRange CR = ICI.makeConstantRange(ICI.getPredicate(), RHSV)
6169 .subtract(LHSV);
6170
6171 if (ICI.isSignedPredicate()) {
6172 if (CR.getLower().isSignBit()) {
6173 return new ICmpInst(ICmpInst::ICMP_SLT, LHSI->getOperand(0),
6174 ConstantInt::get(CR.getUpper()));
6175 } else if (CR.getUpper().isSignBit()) {
6176 return new ICmpInst(ICmpInst::ICMP_SGE, LHSI->getOperand(0),
6177 ConstantInt::get(CR.getLower()));
6178 }
6179 } else {
6180 if (CR.getLower().isMinValue()) {
6181 return new ICmpInst(ICmpInst::ICMP_ULT, LHSI->getOperand(0),
6182 ConstantInt::get(CR.getUpper()));
6183 } else if (CR.getUpper().isMinValue()) {
6184 return new ICmpInst(ICmpInst::ICMP_UGE, LHSI->getOperand(0),
6185 ConstantInt::get(CR.getLower()));
6186 }
6187 }
6188 }
6189 break;
Chris Lattnera74deaf2007-04-03 17:43:25 +00006190 }
6191
6192 // Simplify icmp_eq and icmp_ne instructions with integer constant RHS.
6193 if (ICI.isEquality()) {
6194 bool isICMP_NE = ICI.getPredicate() == ICmpInst::ICMP_NE;
6195
6196 // If the first operand is (add|sub|and|or|xor|rem) with a constant, and
6197 // the second operand is a constant, simplify a bit.
6198 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(LHSI)) {
6199 switch (BO->getOpcode()) {
6200 case Instruction::SRem:
6201 // If we have a signed (X % (2^c)) == 0, turn it into an unsigned one.
6202 if (RHSV == 0 && isa<ConstantInt>(BO->getOperand(1)) &&BO->hasOneUse()){
6203 const APInt &V = cast<ConstantInt>(BO->getOperand(1))->getValue();
6204 if (V.sgt(APInt(V.getBitWidth(), 1)) && V.isPowerOf2()) {
6205 Instruction *NewRem =
6206 BinaryOperator::createURem(BO->getOperand(0), BO->getOperand(1),
6207 BO->getName());
6208 InsertNewInstBefore(NewRem, ICI);
6209 return new ICmpInst(ICI.getPredicate(), NewRem,
6210 Constant::getNullValue(BO->getType()));
6211 }
6212 }
6213 break;
6214 case Instruction::Add:
6215 // Replace ((add A, B) != C) with (A != C-B) if B & C are constants.
6216 if (ConstantInt *BOp1C = dyn_cast<ConstantInt>(BO->getOperand(1))) {
6217 if (BO->hasOneUse())
6218 return new ICmpInst(ICI.getPredicate(), BO->getOperand(0),
6219 Subtract(RHS, BOp1C));
6220 } else if (RHSV == 0) {
6221 // Replace ((add A, B) != 0) with (A != -B) if A or B is
6222 // efficiently invertible, or if the add has just this one use.
6223 Value *BOp0 = BO->getOperand(0), *BOp1 = BO->getOperand(1);
6224
6225 if (Value *NegVal = dyn_castNegVal(BOp1))
6226 return new ICmpInst(ICI.getPredicate(), BOp0, NegVal);
6227 else if (Value *NegVal = dyn_castNegVal(BOp0))
6228 return new ICmpInst(ICI.getPredicate(), NegVal, BOp1);
6229 else if (BO->hasOneUse()) {
6230 Instruction *Neg = BinaryOperator::createNeg(BOp1);
6231 InsertNewInstBefore(Neg, ICI);
6232 Neg->takeName(BO);
6233 return new ICmpInst(ICI.getPredicate(), BOp0, Neg);
6234 }
6235 }
6236 break;
6237 case Instruction::Xor:
6238 // For the xor case, we can xor two constants together, eliminating
6239 // the explicit xor.
6240 if (Constant *BOC = dyn_cast<Constant>(BO->getOperand(1)))
6241 return new ICmpInst(ICI.getPredicate(), BO->getOperand(0),
6242 ConstantExpr::getXor(RHS, BOC));
6243
6244 // FALLTHROUGH
6245 case Instruction::Sub:
6246 // Replace (([sub|xor] A, B) != 0) with (A != B)
6247 if (RHSV == 0)
6248 return new ICmpInst(ICI.getPredicate(), BO->getOperand(0),
6249 BO->getOperand(1));
6250 break;
6251
6252 case Instruction::Or:
6253 // If bits are being or'd in that are not present in the constant we
6254 // are comparing against, then the comparison could never succeed!
6255 if (Constant *BOC = dyn_cast<Constant>(BO->getOperand(1))) {
6256 Constant *NotCI = ConstantExpr::getNot(RHS);
6257 if (!ConstantExpr::getAnd(BOC, NotCI)->isNullValue())
6258 return ReplaceInstUsesWith(ICI, ConstantInt::get(Type::Int1Ty,
6259 isICMP_NE));
6260 }
6261 break;
6262
6263 case Instruction::And:
6264 if (ConstantInt *BOC = dyn_cast<ConstantInt>(BO->getOperand(1))) {
6265 // If bits are being compared against that are and'd out, then the
6266 // comparison can never succeed!
6267 if ((RHSV & ~BOC->getValue()) != 0)
6268 return ReplaceInstUsesWith(ICI, ConstantInt::get(Type::Int1Ty,
6269 isICMP_NE));
6270
6271 // If we have ((X & C) == C), turn it into ((X & C) != 0).
6272 if (RHS == BOC && RHSV.isPowerOf2())
6273 return new ICmpInst(isICMP_NE ? ICmpInst::ICMP_EQ :
6274 ICmpInst::ICMP_NE, LHSI,
6275 Constant::getNullValue(RHS->getType()));
6276
6277 // Replace (and X, (1 << size(X)-1) != 0) with x s< 0
6278 if (isSignBit(BOC)) {
6279 Value *X = BO->getOperand(0);
6280 Constant *Zero = Constant::getNullValue(X->getType());
6281 ICmpInst::Predicate pred = isICMP_NE ?
6282 ICmpInst::ICMP_SLT : ICmpInst::ICMP_SGE;
6283 return new ICmpInst(pred, X, Zero);
6284 }
6285
6286 // ((X & ~7) == 0) --> X < 8
6287 if (RHSV == 0 && isHighOnes(BOC)) {
6288 Value *X = BO->getOperand(0);
6289 Constant *NegX = ConstantExpr::getNeg(BOC);
6290 ICmpInst::Predicate pred = isICMP_NE ?
6291 ICmpInst::ICMP_UGE : ICmpInst::ICMP_ULT;
6292 return new ICmpInst(pred, X, NegX);
6293 }
6294 }
6295 default: break;
6296 }
6297 } else if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(LHSI)) {
6298 // Handle icmp {eq|ne} <intrinsic>, intcst.
6299 if (II->getIntrinsicID() == Intrinsic::bswap) {
6300 AddToWorkList(II);
6301 ICI.setOperand(0, II->getOperand(1));
6302 ICI.setOperand(1, ConstantInt::get(RHSV.byteSwap()));
6303 return &ICI;
6304 }
6305 }
6306 } else { // Not a ICMP_EQ/ICMP_NE
Chris Lattner28d921d2007-04-14 23:32:02 +00006307 // If the LHS is a cast from an integral value of the same size,
6308 // then since we know the RHS is a constant, try to simlify.
Chris Lattnera74deaf2007-04-03 17:43:25 +00006309 if (CastInst *Cast = dyn_cast<CastInst>(LHSI)) {
6310 Value *CastOp = Cast->getOperand(0);
6311 const Type *SrcTy = CastOp->getType();
6312 uint32_t SrcTySize = SrcTy->getPrimitiveSizeInBits();
6313 if (SrcTy->isInteger() &&
6314 SrcTySize == Cast->getType()->getPrimitiveSizeInBits()) {
6315 // If this is an unsigned comparison, try to make the comparison use
6316 // smaller constant values.
6317 if (ICI.getPredicate() == ICmpInst::ICMP_ULT && RHSV.isSignBit()) {
6318 // X u< 128 => X s> -1
6319 return new ICmpInst(ICmpInst::ICMP_SGT, CastOp,
6320 ConstantInt::get(APInt::getAllOnesValue(SrcTySize)));
6321 } else if (ICI.getPredicate() == ICmpInst::ICMP_UGT &&
6322 RHSV == APInt::getSignedMaxValue(SrcTySize)) {
6323 // X u> 127 => X s< 0
6324 return new ICmpInst(ICmpInst::ICMP_SLT, CastOp,
6325 Constant::getNullValue(SrcTy));
6326 }
6327 }
6328 }
6329 }
6330 return 0;
6331}
6332
6333/// visitICmpInstWithCastAndCast - Handle icmp (cast x to y), (cast/cst).
6334/// We only handle extending casts so far.
6335///
Reid Spencer266e42b2006-12-23 06:05:41 +00006336Instruction *InstCombiner::visitICmpInstWithCastAndCast(ICmpInst &ICI) {
6337 const CastInst *LHSCI = cast<CastInst>(ICI.getOperand(0));
Reid Spencer6c38f0b2006-11-27 01:05:10 +00006338 Value *LHSCIOp = LHSCI->getOperand(0);
6339 const Type *SrcTy = LHSCIOp->getType();
Reid Spencer266e42b2006-12-23 06:05:41 +00006340 const Type *DestTy = LHSCI->getType();
Chris Lattnerd1f46d32005-04-24 06:59:08 +00006341 Value *RHSCIOp;
6342
Chris Lattner5aa73fe2007-05-05 22:41:33 +00006343 // Turn icmp (ptrtoint x), (ptrtoint/c) into a compare of the input if the
6344 // integer type is the same size as the pointer type.
6345 if (LHSCI->getOpcode() == Instruction::PtrToInt &&
6346 getTargetData().getPointerSizeInBits() ==
6347 cast<IntegerType>(DestTy)->getBitWidth()) {
6348 Value *RHSOp = 0;
6349 if (Constant *RHSC = dyn_cast<Constant>(ICI.getOperand(1))) {
Chris Lattner9b35b3e2007-05-06 07:24:03 +00006350 RHSOp = ConstantExpr::getIntToPtr(RHSC, SrcTy);
Chris Lattner5aa73fe2007-05-05 22:41:33 +00006351 } else if (PtrToIntInst *RHSC = dyn_cast<PtrToIntInst>(ICI.getOperand(1))) {
6352 RHSOp = RHSC->getOperand(0);
6353 // If the pointer types don't match, insert a bitcast.
6354 if (LHSCIOp->getType() != RHSOp->getType())
Chris Lattner5a866122008-01-13 22:23:22 +00006355 RHSOp = InsertBitCastBefore(RHSOp, LHSCIOp->getType(), ICI);
Chris Lattner5aa73fe2007-05-05 22:41:33 +00006356 }
6357
6358 if (RHSOp)
6359 return new ICmpInst(ICI.getPredicate(), LHSCIOp, RHSOp);
6360 }
6361
6362 // The code below only handles extension cast instructions, so far.
6363 // Enforce this.
Reid Spencer266e42b2006-12-23 06:05:41 +00006364 if (LHSCI->getOpcode() != Instruction::ZExt &&
6365 LHSCI->getOpcode() != Instruction::SExt)
Chris Lattner03f06f12005-01-17 03:20:02 +00006366 return 0;
6367
Reid Spencer266e42b2006-12-23 06:05:41 +00006368 bool isSignedExt = LHSCI->getOpcode() == Instruction::SExt;
6369 bool isSignedCmp = ICI.isSignedPredicate();
Chris Lattnerd1f46d32005-04-24 06:59:08 +00006370
Reid Spencer266e42b2006-12-23 06:05:41 +00006371 if (CastInst *CI = dyn_cast<CastInst>(ICI.getOperand(1))) {
Chris Lattnerd1f46d32005-04-24 06:59:08 +00006372 // Not an extension from the same type?
6373 RHSCIOp = CI->getOperand(0);
Reid Spencer266e42b2006-12-23 06:05:41 +00006374 if (RHSCIOp->getType() != LHSCIOp->getType())
6375 return 0;
Chris Lattner387bf3f2007-01-13 23:11:38 +00006376
Nick Lewycky8ea81e82008-01-28 03:48:02 +00006377 // If the signedness of the two casts doesn't agree (i.e. one is a sext
Chris Lattner387bf3f2007-01-13 23:11:38 +00006378 // and the other is a zext), then we can't handle this.
6379 if (CI->getOpcode() != LHSCI->getOpcode())
6380 return 0;
6381
Nick Lewycky8ea81e82008-01-28 03:48:02 +00006382 // Deal with equality cases early.
6383 if (ICI.isEquality())
6384 return new ICmpInst(ICI.getPredicate(), LHSCIOp, RHSCIOp);
6385
6386 // A signed comparison of sign extended values simplifies into a
6387 // signed comparison.
6388 if (isSignedCmp && isSignedExt)
6389 return new ICmpInst(ICI.getPredicate(), LHSCIOp, RHSCIOp);
6390
6391 // The other three cases all fold into an unsigned comparison.
6392 return new ICmpInst(ICI.getUnsignedPredicate(), LHSCIOp, RHSCIOp);
Reid Spencer279fa252004-11-28 21:31:15 +00006393 }
Chris Lattnerf4cdbf32002-05-06 16:14:14 +00006394
Reid Spencer266e42b2006-12-23 06:05:41 +00006395 // If we aren't dealing with a constant on the RHS, exit early
6396 ConstantInt *CI = dyn_cast<ConstantInt>(ICI.getOperand(1));
6397 if (!CI)
6398 return 0;
6399
6400 // Compute the constant that would happen if we truncated to SrcTy then
6401 // reextended to DestTy.
6402 Constant *Res1 = ConstantExpr::getTrunc(CI, SrcTy);
6403 Constant *Res2 = ConstantExpr::getCast(LHSCI->getOpcode(), Res1, DestTy);
6404
6405 // If the re-extended constant didn't change...
6406 if (Res2 == CI) {
6407 // Make sure that sign of the Cmp and the sign of the Cast are the same.
6408 // For example, we might have:
6409 // %A = sext short %X to uint
6410 // %B = icmp ugt uint %A, 1330
6411 // It is incorrect to transform this into
6412 // %B = icmp ugt short %X, 1330
6413 // because %A may have negative value.
6414 //
6415 // However, it is OK if SrcTy is bool (See cast-set.ll testcase)
6416 // OR operation is EQ/NE.
Reid Spencer542964f2007-01-11 18:21:29 +00006417 if (isSignedExt == isSignedCmp || SrcTy == Type::Int1Ty || ICI.isEquality())
Reid Spencer266e42b2006-12-23 06:05:41 +00006418 return new ICmpInst(ICI.getPredicate(), LHSCIOp, Res1);
6419 else
6420 return 0;
6421 }
6422
6423 // The re-extended constant changed so the constant cannot be represented
6424 // in the shorter type. Consequently, we cannot emit a simple comparison.
6425
6426 // First, handle some easy cases. We know the result cannot be equal at this
6427 // point so handle the ICI.isEquality() cases
6428 if (ICI.getPredicate() == ICmpInst::ICMP_EQ)
Zhou Sheng75b871f2007-01-11 12:24:14 +00006429 return ReplaceInstUsesWith(ICI, ConstantInt::getFalse());
Reid Spencer266e42b2006-12-23 06:05:41 +00006430 if (ICI.getPredicate() == ICmpInst::ICMP_NE)
Zhou Sheng75b871f2007-01-11 12:24:14 +00006431 return ReplaceInstUsesWith(ICI, ConstantInt::getTrue());
Reid Spencer266e42b2006-12-23 06:05:41 +00006432
6433 // Evaluate the comparison for LT (we invert for GT below). LE and GE cases
6434 // should have been folded away previously and not enter in here.
6435 Value *Result;
6436 if (isSignedCmp) {
6437 // We're performing a signed comparison.
Reid Spencerc3e3b8a2007-03-22 20:36:03 +00006438 if (cast<ConstantInt>(CI)->getValue().isNegative())
Zhou Sheng75b871f2007-01-11 12:24:14 +00006439 Result = ConstantInt::getFalse(); // X < (small) --> false
Reid Spencer266e42b2006-12-23 06:05:41 +00006440 else
Zhou Sheng75b871f2007-01-11 12:24:14 +00006441 Result = ConstantInt::getTrue(); // X < (large) --> true
Reid Spencer266e42b2006-12-23 06:05:41 +00006442 } else {
6443 // We're performing an unsigned comparison.
6444 if (isSignedExt) {
6445 // We're performing an unsigned comp with a sign extended value.
6446 // This is true if the input is >= 0. [aka >s -1]
Zhou Sheng75b871f2007-01-11 12:24:14 +00006447 Constant *NegOne = ConstantInt::getAllOnesValue(SrcTy);
Reid Spencer266e42b2006-12-23 06:05:41 +00006448 Result = InsertNewInstBefore(new ICmpInst(ICmpInst::ICMP_SGT, LHSCIOp,
6449 NegOne, ICI.getName()), ICI);
6450 } else {
6451 // Unsigned extend & unsigned compare -> always true.
Zhou Sheng75b871f2007-01-11 12:24:14 +00006452 Result = ConstantInt::getTrue();
Reid Spencer266e42b2006-12-23 06:05:41 +00006453 }
6454 }
6455
6456 // Finally, return the value computed.
6457 if (ICI.getPredicate() == ICmpInst::ICMP_ULT ||
6458 ICI.getPredicate() == ICmpInst::ICMP_SLT) {
6459 return ReplaceInstUsesWith(ICI, Result);
6460 } else {
6461 assert((ICI.getPredicate()==ICmpInst::ICMP_UGT ||
6462 ICI.getPredicate()==ICmpInst::ICMP_SGT) &&
6463 "ICmp should be folded!");
6464 if (Constant *CI = dyn_cast<Constant>(Result))
6465 return ReplaceInstUsesWith(ICI, ConstantExpr::getNot(CI));
6466 else
6467 return BinaryOperator::createNot(Result);
6468 }
Chris Lattnerd1f46d32005-04-24 06:59:08 +00006469}
Chris Lattnerf4cdbf32002-05-06 16:14:14 +00006470
Reid Spencer2341c222007-02-02 02:16:23 +00006471Instruction *InstCombiner::visitShl(BinaryOperator &I) {
6472 return commonShiftTransforms(I);
6473}
6474
6475Instruction *InstCombiner::visitLShr(BinaryOperator &I) {
6476 return commonShiftTransforms(I);
6477}
6478
6479Instruction *InstCombiner::visitAShr(BinaryOperator &I) {
Chris Lattner0ccb6632007-12-06 01:59:46 +00006480 if (Instruction *R = commonShiftTransforms(I))
6481 return R;
6482
6483 Value *Op0 = I.getOperand(0);
6484
6485 // ashr int -1, X = -1 (for any arithmetic shift rights of ~0)
6486 if (ConstantInt *CSI = dyn_cast<ConstantInt>(Op0))
6487 if (CSI->isAllOnesValue())
6488 return ReplaceInstUsesWith(I, CSI);
6489
6490 // See if we can turn a signed shr into an unsigned shr.
6491 if (MaskedValueIsZero(Op0,
6492 APInt::getSignBit(I.getType()->getPrimitiveSizeInBits())))
6493 return BinaryOperator::createLShr(Op0, I.getOperand(1));
6494
6495 return 0;
Reid Spencer2341c222007-02-02 02:16:23 +00006496}
6497
6498Instruction *InstCombiner::commonShiftTransforms(BinaryOperator &I) {
6499 assert(I.getOperand(1)->getType() == I.getOperand(0)->getType());
Chris Lattner113f4f42002-06-25 16:13:24 +00006500 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
Chris Lattnerf4cdbf32002-05-06 16:14:14 +00006501
6502 // shl X, 0 == X and shr X, 0 == X
6503 // shl 0, X == 0 and shr 0, X == 0
Reid Spencer2341c222007-02-02 02:16:23 +00006504 if (Op1 == Constant::getNullValue(Op1->getType()) ||
Chris Lattnere6794492002-08-12 21:17:25 +00006505 Op0 == Constant::getNullValue(Op0->getType()))
6506 return ReplaceInstUsesWith(I, Op0);
Chris Lattnerf5b4ef72006-02-12 08:07:37 +00006507
Reid Spencer266e42b2006-12-23 06:05:41 +00006508 if (isa<UndefValue>(Op0)) {
6509 if (I.getOpcode() == Instruction::AShr) // undef >>s X -> undef
Chris Lattner67f05452004-10-16 23:28:04 +00006510 return ReplaceInstUsesWith(I, Op0);
Reid Spencer266e42b2006-12-23 06:05:41 +00006511 else // undef << X -> 0, undef >>u X -> 0
Chris Lattner81a7a232004-10-16 18:11:37 +00006512 return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
6513 }
6514 if (isa<UndefValue>(Op1)) {
Reid Spencer266e42b2006-12-23 06:05:41 +00006515 if (I.getOpcode() == Instruction::AShr) // X >>s undef -> X
6516 return ReplaceInstUsesWith(I, Op0);
6517 else // X << undef, X >>u undef -> 0
Chris Lattner81a7a232004-10-16 18:11:37 +00006518 return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
Chris Lattner81a7a232004-10-16 18:11:37 +00006519 }
6520
Chris Lattner183b3362004-04-09 19:05:30 +00006521 // Try to fold constant and into select arguments.
6522 if (isa<Constant>(Op0))
6523 if (SelectInst *SI = dyn_cast<SelectInst>(Op1))
Chris Lattner86102b82005-01-01 16:22:27 +00006524 if (Instruction *R = FoldOpIntoSelect(I, SI, this))
Chris Lattner183b3362004-04-09 19:05:30 +00006525 return R;
6526
Reid Spencere0fc4df2006-10-20 07:07:24 +00006527 if (ConstantInt *CUI = dyn_cast<ConstantInt>(Op1))
Reid Spencerc635f472006-12-31 05:48:39 +00006528 if (Instruction *Res = FoldShiftByConstant(Op0, CUI, I))
6529 return Res;
Chris Lattner14553932006-01-06 07:12:35 +00006530 return 0;
6531}
6532
Reid Spencere0fc4df2006-10-20 07:07:24 +00006533Instruction *InstCombiner::FoldShiftByConstant(Value *Op0, ConstantInt *Op1,
Reid Spencer2341c222007-02-02 02:16:23 +00006534 BinaryOperator &I) {
Reid Spencer266e42b2006-12-23 06:05:41 +00006535 bool isLeftShift = I.getOpcode() == Instruction::Shl;
Chris Lattner14553932006-01-06 07:12:35 +00006536
Chris Lattnerf5b4ef72006-02-12 08:07:37 +00006537 // See if we can simplify any instructions used by the instruction whose sole
6538 // purpose is to compute bits we don't care about.
Reid Spencer6274c722007-03-23 18:46:34 +00006539 uint32_t TypeBits = Op0->getType()->getPrimitiveSizeInBits();
6540 APInt KnownZero(TypeBits, 0), KnownOne(TypeBits, 0);
6541 if (SimplifyDemandedBits(&I, APInt::getAllOnesValue(TypeBits),
Chris Lattnerf5b4ef72006-02-12 08:07:37 +00006542 KnownZero, KnownOne))
6543 return &I;
6544
Chris Lattner14553932006-01-06 07:12:35 +00006545 // shl uint X, 32 = 0 and shr ubyte Y, 9 = 0, ... just don't eliminate shr
6546 // of a signed value.
6547 //
Zhou Shengb25806f2007-03-30 09:29:48 +00006548 if (Op1->uge(TypeBits)) {
Chris Lattnerd5fea612007-02-02 05:29:55 +00006549 if (I.getOpcode() != Instruction::AShr)
Chris Lattner14553932006-01-06 07:12:35 +00006550 return ReplaceInstUsesWith(I, Constant::getNullValue(Op0->getType()));
6551 else {
Chris Lattnerd5fea612007-02-02 05:29:55 +00006552 I.setOperand(1, ConstantInt::get(I.getType(), TypeBits-1));
Chris Lattner14553932006-01-06 07:12:35 +00006553 return &I;
Chris Lattnerf5ce2542004-02-23 20:30:06 +00006554 }
Chris Lattner14553932006-01-06 07:12:35 +00006555 }
6556
6557 // ((X*C1) << C2) == (X * (C1 << C2))
6558 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Op0))
6559 if (BO->getOpcode() == Instruction::Mul && isLeftShift)
6560 if (Constant *BOOp = dyn_cast<Constant>(BO->getOperand(1)))
6561 return BinaryOperator::createMul(BO->getOperand(0),
6562 ConstantExpr::getShl(BOOp, Op1));
6563
6564 // Try to fold constant and into select arguments.
6565 if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
6566 if (Instruction *R = FoldOpIntoSelect(I, SI, this))
6567 return R;
6568 if (isa<PHINode>(Op0))
6569 if (Instruction *NV = FoldOpIntoPhi(I))
6570 return NV;
6571
Chris Lattner74b2ab52007-12-22 09:07:47 +00006572 // Fold shift2(trunc(shift1(x,c1)), c2) -> trunc(shift2(shift1(x,c1),c2))
6573 if (TruncInst *TI = dyn_cast<TruncInst>(Op0)) {
6574 Instruction *TrOp = dyn_cast<Instruction>(TI->getOperand(0));
6575 // If 'shift2' is an ashr, we would have to get the sign bit into a funny
6576 // place. Don't try to do this transformation in this case. Also, we
6577 // require that the input operand is a shift-by-constant so that we have
6578 // confidence that the shifts will get folded together. We could do this
6579 // xform in more cases, but it is unlikely to be profitable.
6580 if (TrOp && I.isLogicalShift() && TrOp->isShift() &&
6581 isa<ConstantInt>(TrOp->getOperand(1))) {
6582 // Okay, we'll do this xform. Make the shift of shift.
6583 Constant *ShAmt = ConstantExpr::getZExt(Op1, TrOp->getType());
6584 Instruction *NSh = BinaryOperator::create(I.getOpcode(), TrOp, ShAmt,
6585 I.getName());
6586 InsertNewInstBefore(NSh, I); // (shift2 (shift1 & 0x00FF), c2)
6587
6588 // For logical shifts, the truncation has the effect of making the high
6589 // part of the register be zeros. Emulate this by inserting an AND to
6590 // clear the top bits as needed. This 'and' will usually be zapped by
6591 // other xforms later if dead.
6592 unsigned SrcSize = TrOp->getType()->getPrimitiveSizeInBits();
6593 unsigned DstSize = TI->getType()->getPrimitiveSizeInBits();
6594 APInt MaskV(APInt::getLowBitsSet(SrcSize, DstSize));
6595
6596 // The mask we constructed says what the trunc would do if occurring
6597 // between the shifts. We want to know the effect *after* the second
6598 // shift. We know that it is a logical shift by a constant, so adjust the
6599 // mask as appropriate.
6600 if (I.getOpcode() == Instruction::Shl)
6601 MaskV <<= Op1->getZExtValue();
6602 else {
6603 assert(I.getOpcode() == Instruction::LShr && "Unknown logical shift");
6604 MaskV = MaskV.lshr(Op1->getZExtValue());
6605 }
6606
6607 Instruction *And = BinaryOperator::createAnd(NSh, ConstantInt::get(MaskV),
6608 TI->getName());
6609 InsertNewInstBefore(And, I); // shift1 & 0x00FF
6610
6611 // Return the value truncated to the interesting size.
6612 return new TruncInst(And, I.getType());
6613 }
6614 }
6615
Chris Lattner14553932006-01-06 07:12:35 +00006616 if (Op0->hasOneUse()) {
Chris Lattner14553932006-01-06 07:12:35 +00006617 if (BinaryOperator *Op0BO = dyn_cast<BinaryOperator>(Op0)) {
6618 // Turn ((X >> C) + Y) << C -> (X + (Y << C)) & (~0 << C)
6619 Value *V1, *V2;
6620 ConstantInt *CC;
6621 switch (Op0BO->getOpcode()) {
Chris Lattner27cb9db2005-09-18 05:12:10 +00006622 default: break;
6623 case Instruction::Add:
6624 case Instruction::And:
6625 case Instruction::Or:
Reid Spencer2f34b982007-02-02 14:41:37 +00006626 case Instruction::Xor: {
Chris Lattner27cb9db2005-09-18 05:12:10 +00006627 // These operators commute.
6628 // Turn (Y + (X >> C)) << C -> (X + (Y << C)) & (~0 << C)
Chris Lattner797dee72005-09-18 06:30:59 +00006629 if (isLeftShift && Op0BO->getOperand(1)->hasOneUse() &&
6630 match(Op0BO->getOperand(1),
Chris Lattner14553932006-01-06 07:12:35 +00006631 m_Shr(m_Value(V1), m_ConstantInt(CC))) && CC == Op1) {
Reid Spencer0d5f9232007-02-02 14:08:20 +00006632 Instruction *YS = BinaryOperator::createShl(
Chris Lattner14553932006-01-06 07:12:35 +00006633 Op0BO->getOperand(0), Op1,
Chris Lattner797dee72005-09-18 06:30:59 +00006634 Op0BO->getName());
6635 InsertNewInstBefore(YS, I); // (Y << C)
Chris Lattner24cd2fa2006-02-09 07:41:14 +00006636 Instruction *X =
6637 BinaryOperator::create(Op0BO->getOpcode(), YS, V1,
6638 Op0BO->getOperand(1)->getName());
Chris Lattner797dee72005-09-18 06:30:59 +00006639 InsertNewInstBefore(X, I); // (X + (Y << C))
Zhou Shengfd28a332007-03-30 17:20:39 +00006640 uint32_t Op1Val = Op1->getLimitedValue(TypeBits);
Zhou Sheng5e60a4a2007-03-30 05:45:18 +00006641 return BinaryOperator::createAnd(X, ConstantInt::get(
6642 APInt::getHighBitsSet(TypeBits, TypeBits-Op1Val)));
Chris Lattner797dee72005-09-18 06:30:59 +00006643 }
Chris Lattner14553932006-01-06 07:12:35 +00006644
Chris Lattner797dee72005-09-18 06:30:59 +00006645 // Turn (Y + ((X >> C) & CC)) << C -> ((X & (CC << C)) + (Y << C))
Reid Spencer2f34b982007-02-02 14:41:37 +00006646 Value *Op0BOOp1 = Op0BO->getOperand(1);
Chris Lattnerfe53cf22007-03-05 00:11:19 +00006647 if (isLeftShift && Op0BOOp1->hasOneUse() &&
Reid Spencer2f34b982007-02-02 14:41:37 +00006648 match(Op0BOOp1,
6649 m_And(m_Shr(m_Value(V1), m_Value(V2)),m_ConstantInt(CC))) &&
Chris Lattnerfe53cf22007-03-05 00:11:19 +00006650 cast<BinaryOperator>(Op0BOOp1)->getOperand(0)->hasOneUse() &&
6651 V2 == Op1) {
Reid Spencer0d5f9232007-02-02 14:08:20 +00006652 Instruction *YS = BinaryOperator::createShl(
Reid Spencer2341c222007-02-02 02:16:23 +00006653 Op0BO->getOperand(0), Op1,
6654 Op0BO->getName());
Chris Lattner797dee72005-09-18 06:30:59 +00006655 InsertNewInstBefore(YS, I); // (Y << C)
6656 Instruction *XM =
Chris Lattner14553932006-01-06 07:12:35 +00006657 BinaryOperator::createAnd(V1, ConstantExpr::getShl(CC, Op1),
Chris Lattner797dee72005-09-18 06:30:59 +00006658 V1->getName()+".mask");
6659 InsertNewInstBefore(XM, I); // X & (CC << C)
6660
6661 return BinaryOperator::create(Op0BO->getOpcode(), YS, XM);
6662 }
Reid Spencer2f34b982007-02-02 14:41:37 +00006663 }
Chris Lattner14553932006-01-06 07:12:35 +00006664
Reid Spencer2f34b982007-02-02 14:41:37 +00006665 // FALL THROUGH.
6666 case Instruction::Sub: {
Chris Lattner27cb9db2005-09-18 05:12:10 +00006667 // Turn ((X >> C) + Y) << C -> (X + (Y << C)) & (~0 << C)
Chris Lattner797dee72005-09-18 06:30:59 +00006668 if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() &&
6669 match(Op0BO->getOperand(0),
Chris Lattner14553932006-01-06 07:12:35 +00006670 m_Shr(m_Value(V1), m_ConstantInt(CC))) && CC == Op1) {
Reid Spencer0d5f9232007-02-02 14:08:20 +00006671 Instruction *YS = BinaryOperator::createShl(
Reid Spencer2341c222007-02-02 02:16:23 +00006672 Op0BO->getOperand(1), Op1,
6673 Op0BO->getName());
Chris Lattner797dee72005-09-18 06:30:59 +00006674 InsertNewInstBefore(YS, I); // (Y << C)
Chris Lattner24cd2fa2006-02-09 07:41:14 +00006675 Instruction *X =
Chris Lattner1df0e982006-05-31 21:14:00 +00006676 BinaryOperator::create(Op0BO->getOpcode(), V1, YS,
Chris Lattner24cd2fa2006-02-09 07:41:14 +00006677 Op0BO->getOperand(0)->getName());
Chris Lattner797dee72005-09-18 06:30:59 +00006678 InsertNewInstBefore(X, I); // (X + (Y << C))
Zhou Shengfd28a332007-03-30 17:20:39 +00006679 uint32_t Op1Val = Op1->getLimitedValue(TypeBits);
Zhou Sheng5e60a4a2007-03-30 05:45:18 +00006680 return BinaryOperator::createAnd(X, ConstantInt::get(
6681 APInt::getHighBitsSet(TypeBits, TypeBits-Op1Val)));
Chris Lattner797dee72005-09-18 06:30:59 +00006682 }
Chris Lattner14553932006-01-06 07:12:35 +00006683
Chris Lattner1df0e982006-05-31 21:14:00 +00006684 // Turn (((X >> C)&CC) + Y) << C -> (X + (Y << C)) & (CC << C)
Chris Lattner797dee72005-09-18 06:30:59 +00006685 if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() &&
6686 match(Op0BO->getOperand(0),
6687 m_And(m_Shr(m_Value(V1), m_Value(V2)),
Chris Lattner14553932006-01-06 07:12:35 +00006688 m_ConstantInt(CC))) && V2 == Op1 &&
Chris Lattner24cd2fa2006-02-09 07:41:14 +00006689 cast<BinaryOperator>(Op0BO->getOperand(0))
6690 ->getOperand(0)->hasOneUse()) {
Reid Spencer0d5f9232007-02-02 14:08:20 +00006691 Instruction *YS = BinaryOperator::createShl(
Reid Spencer2341c222007-02-02 02:16:23 +00006692 Op0BO->getOperand(1), Op1,
6693 Op0BO->getName());
Chris Lattner797dee72005-09-18 06:30:59 +00006694 InsertNewInstBefore(YS, I); // (Y << C)
6695 Instruction *XM =
Chris Lattner14553932006-01-06 07:12:35 +00006696 BinaryOperator::createAnd(V1, ConstantExpr::getShl(CC, Op1),
Chris Lattner797dee72005-09-18 06:30:59 +00006697 V1->getName()+".mask");
6698 InsertNewInstBefore(XM, I); // X & (CC << C)
6699
Chris Lattner1df0e982006-05-31 21:14:00 +00006700 return BinaryOperator::create(Op0BO->getOpcode(), XM, YS);
Chris Lattner797dee72005-09-18 06:30:59 +00006701 }
Chris Lattner14553932006-01-06 07:12:35 +00006702
Chris Lattner27cb9db2005-09-18 05:12:10 +00006703 break;
Reid Spencer2f34b982007-02-02 14:41:37 +00006704 }
Chris Lattner14553932006-01-06 07:12:35 +00006705 }
6706
6707
6708 // If the operand is an bitwise operator with a constant RHS, and the
6709 // shift is the only use, we can pull it out of the shift.
6710 if (ConstantInt *Op0C = dyn_cast<ConstantInt>(Op0BO->getOperand(1))) {
6711 bool isValid = true; // Valid only for And, Or, Xor
6712 bool highBitSet = false; // Transform if high bit of constant set?
6713
6714 switch (Op0BO->getOpcode()) {
Chris Lattnerdeaa0dd2003-08-12 21:53:41 +00006715 default: isValid = false; break; // Do not perform transform!
Chris Lattner44bd3922004-10-08 03:46:20 +00006716 case Instruction::Add:
6717 isValid = isLeftShift;
6718 break;
Chris Lattnerdeaa0dd2003-08-12 21:53:41 +00006719 case Instruction::Or:
6720 case Instruction::Xor:
6721 highBitSet = false;
6722 break;
6723 case Instruction::And:
6724 highBitSet = true;
6725 break;
Chris Lattner14553932006-01-06 07:12:35 +00006726 }
6727
6728 // If this is a signed shift right, and the high bit is modified
6729 // by the logical operation, do not perform the transformation.
6730 // The highBitSet boolean indicates the value of the high bit of
6731 // the constant which would cause it to be modified for this
6732 // operation.
6733 //
Chris Lattnerd2bbbab2007-12-06 06:25:04 +00006734 if (isValid && I.getOpcode() == Instruction::AShr)
Zhou Sheng23f7a1c2007-03-28 15:02:20 +00006735 isValid = Op0C->getValue()[TypeBits-1] == highBitSet;
Chris Lattner14553932006-01-06 07:12:35 +00006736
6737 if (isValid) {
6738 Constant *NewRHS = ConstantExpr::get(I.getOpcode(), Op0C, Op1);
6739
6740 Instruction *NewShift =
Chris Lattner6e0123b2007-02-11 01:23:03 +00006741 BinaryOperator::create(I.getOpcode(), Op0BO->getOperand(0), Op1);
Chris Lattner14553932006-01-06 07:12:35 +00006742 InsertNewInstBefore(NewShift, I);
Chris Lattner6e0123b2007-02-11 01:23:03 +00006743 NewShift->takeName(Op0BO);
Chris Lattner14553932006-01-06 07:12:35 +00006744
6745 return BinaryOperator::create(Op0BO->getOpcode(), NewShift,
6746 NewRHS);
6747 }
6748 }
6749 }
6750 }
6751
Chris Lattnereb372a02006-01-06 07:52:12 +00006752 // Find out if this is a shift of a shift by a constant.
Reid Spencer2341c222007-02-02 02:16:23 +00006753 BinaryOperator *ShiftOp = dyn_cast<BinaryOperator>(Op0);
6754 if (ShiftOp && !ShiftOp->isShift())
6755 ShiftOp = 0;
Chris Lattnereb372a02006-01-06 07:52:12 +00006756
Reid Spencere0fc4df2006-10-20 07:07:24 +00006757 if (ShiftOp && isa<ConstantInt>(ShiftOp->getOperand(1))) {
Reid Spencere0fc4df2006-10-20 07:07:24 +00006758 ConstantInt *ShiftAmt1C = cast<ConstantInt>(ShiftOp->getOperand(1));
Zhou Shengb25806f2007-03-30 09:29:48 +00006759 uint32_t ShiftAmt1 = ShiftAmt1C->getLimitedValue(TypeBits);
6760 uint32_t ShiftAmt2 = Op1->getLimitedValue(TypeBits);
Chris Lattner3e009e82007-02-05 00:57:54 +00006761 assert(ShiftAmt2 != 0 && "Should have been simplified earlier");
6762 if (ShiftAmt1 == 0) return 0; // Will be simplified in the future.
6763 Value *X = ShiftOp->getOperand(0);
Chris Lattnereb372a02006-01-06 07:52:12 +00006764
Zhou Sheng56cda952007-04-02 08:20:41 +00006765 uint32_t AmtSum = ShiftAmt1+ShiftAmt2; // Fold into one big shift.
Reid Spencer6274c722007-03-23 18:46:34 +00006766 if (AmtSum > TypeBits)
6767 AmtSum = TypeBits;
Chris Lattner3e009e82007-02-05 00:57:54 +00006768
6769 const IntegerType *Ty = cast<IntegerType>(I.getType());
6770
6771 // Check for (X << c1) << c2 and (X >> c1) >> c2
Chris Lattner6c344e52007-02-03 23:28:07 +00006772 if (I.getOpcode() == ShiftOp->getOpcode()) {
Chris Lattner3e009e82007-02-05 00:57:54 +00006773 return BinaryOperator::create(I.getOpcode(), X,
6774 ConstantInt::get(Ty, AmtSum));
6775 } else if (ShiftOp->getOpcode() == Instruction::LShr &&
6776 I.getOpcode() == Instruction::AShr) {
6777 // ((X >>u C1) >>s C2) -> (X >>u (C1+C2)) since C1 != 0.
6778 return BinaryOperator::createLShr(X, ConstantInt::get(Ty, AmtSum));
6779 } else if (ShiftOp->getOpcode() == Instruction::AShr &&
6780 I.getOpcode() == Instruction::LShr) {
6781 // ((X >>s C1) >>u C2) -> ((X >>s (C1+C2)) & mask) since C1 != 0.
6782 Instruction *Shift =
6783 BinaryOperator::createAShr(X, ConstantInt::get(Ty, AmtSum));
6784 InsertNewInstBefore(Shift, I);
6785
Zhou Sheng23f7a1c2007-03-28 15:02:20 +00006786 APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2));
Reid Spencer6274c722007-03-23 18:46:34 +00006787 return BinaryOperator::createAnd(Shift, ConstantInt::get(Mask));
Chris Lattnereb372a02006-01-06 07:52:12 +00006788 }
6789
Chris Lattner3e009e82007-02-05 00:57:54 +00006790 // Okay, if we get here, one shift must be left, and the other shift must be
6791 // right. See if the amounts are equal.
6792 if (ShiftAmt1 == ShiftAmt2) {
6793 // If we have ((X >>? C) << C), turn this into X & (-1 << C).
6794 if (I.getOpcode() == Instruction::Shl) {
Reid Spencer52830322007-03-25 21:11:44 +00006795 APInt Mask(APInt::getHighBitsSet(TypeBits, TypeBits - ShiftAmt1));
Reid Spencer6274c722007-03-23 18:46:34 +00006796 return BinaryOperator::createAnd(X, ConstantInt::get(Mask));
Chris Lattner3e009e82007-02-05 00:57:54 +00006797 }
6798 // If we have ((X << C) >>u C), turn this into X & (-1 >>u C).
6799 if (I.getOpcode() == Instruction::LShr) {
Zhou Sheng150f3bb2007-04-01 17:13:37 +00006800 APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt1));
Reid Spencer6274c722007-03-23 18:46:34 +00006801 return BinaryOperator::createAnd(X, ConstantInt::get(Mask));
Chris Lattner3e009e82007-02-05 00:57:54 +00006802 }
6803 // We can simplify ((X << C) >>s C) into a trunc + sext.
6804 // NOTE: we could do this for any C, but that would make 'unusual' integer
6805 // types. For now, just stick to ones well-supported by the code
6806 // generators.
6807 const Type *SExtType = 0;
6808 switch (Ty->getBitWidth() - ShiftAmt1) {
Zhou Sheng23f7a1c2007-03-28 15:02:20 +00006809 case 1 :
6810 case 8 :
6811 case 16 :
6812 case 32 :
6813 case 64 :
6814 case 128:
6815 SExtType = IntegerType::get(Ty->getBitWidth() - ShiftAmt1);
6816 break;
Chris Lattner3e009e82007-02-05 00:57:54 +00006817 default: break;
6818 }
6819 if (SExtType) {
6820 Instruction *NewTrunc = new TruncInst(X, SExtType, "sext");
6821 InsertNewInstBefore(NewTrunc, I);
6822 return new SExtInst(NewTrunc, Ty);
6823 }
6824 // Otherwise, we can't handle it yet.
6825 } else if (ShiftAmt1 < ShiftAmt2) {
Zhou Sheng56cda952007-04-02 08:20:41 +00006826 uint32_t ShiftDiff = ShiftAmt2-ShiftAmt1;
Chris Lattnereb372a02006-01-06 07:52:12 +00006827
Chris Lattner83ac5ae92007-02-05 05:57:49 +00006828 // (X >>? C1) << C2 --> X << (C2-C1) & (-1 << C2)
Chris Lattner3e009e82007-02-05 00:57:54 +00006829 if (I.getOpcode() == Instruction::Shl) {
6830 assert(ShiftOp->getOpcode() == Instruction::LShr ||
6831 ShiftOp->getOpcode() == Instruction::AShr);
Chris Lattner9cbfbc22006-01-07 01:32:28 +00006832 Instruction *Shift =
Chris Lattner3e009e82007-02-05 00:57:54 +00006833 BinaryOperator::createShl(X, ConstantInt::get(Ty, ShiftDiff));
Chris Lattner9cbfbc22006-01-07 01:32:28 +00006834 InsertNewInstBefore(Shift, I);
6835
Reid Spencer52830322007-03-25 21:11:44 +00006836 APInt Mask(APInt::getHighBitsSet(TypeBits, TypeBits - ShiftAmt2));
6837 return BinaryOperator::createAnd(Shift, ConstantInt::get(Mask));
Chris Lattnereb372a02006-01-06 07:52:12 +00006838 }
Chris Lattner3e009e82007-02-05 00:57:54 +00006839
Chris Lattner83ac5ae92007-02-05 05:57:49 +00006840 // (X << C1) >>u C2 --> X >>u (C2-C1) & (-1 >> C2)
Chris Lattner3e009e82007-02-05 00:57:54 +00006841 if (I.getOpcode() == Instruction::LShr) {
6842 assert(ShiftOp->getOpcode() == Instruction::Shl);
6843 Instruction *Shift =
6844 BinaryOperator::createLShr(X, ConstantInt::get(Ty, ShiftDiff));
6845 InsertNewInstBefore(Shift, I);
Chris Lattnereb372a02006-01-06 07:52:12 +00006846
Reid Spencer769a5a82007-03-26 17:18:58 +00006847 APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2));
Reid Spencer6274c722007-03-23 18:46:34 +00006848 return BinaryOperator::createAnd(Shift, ConstantInt::get(Mask));
Chris Lattner27cb9db2005-09-18 05:12:10 +00006849 }
Chris Lattner3e009e82007-02-05 00:57:54 +00006850
6851 // We can't handle (X << C1) >>s C2, it shifts arbitrary bits in.
6852 } else {
6853 assert(ShiftAmt2 < ShiftAmt1);
Zhou Sheng56cda952007-04-02 08:20:41 +00006854 uint32_t ShiftDiff = ShiftAmt1-ShiftAmt2;
Chris Lattner3e009e82007-02-05 00:57:54 +00006855
Chris Lattner83ac5ae92007-02-05 05:57:49 +00006856 // (X >>? C1) << C2 --> X >>? (C1-C2) & (-1 << C2)
Chris Lattner3e009e82007-02-05 00:57:54 +00006857 if (I.getOpcode() == Instruction::Shl) {
6858 assert(ShiftOp->getOpcode() == Instruction::LShr ||
6859 ShiftOp->getOpcode() == Instruction::AShr);
6860 Instruction *Shift =
6861 BinaryOperator::create(ShiftOp->getOpcode(), X,
6862 ConstantInt::get(Ty, ShiftDiff));
6863 InsertNewInstBefore(Shift, I);
6864
Reid Spencer52830322007-03-25 21:11:44 +00006865 APInt Mask(APInt::getHighBitsSet(TypeBits, TypeBits - ShiftAmt2));
Reid Spencer6274c722007-03-23 18:46:34 +00006866 return BinaryOperator::createAnd(Shift, ConstantInt::get(Mask));
Chris Lattner3e009e82007-02-05 00:57:54 +00006867 }
6868
Chris Lattner83ac5ae92007-02-05 05:57:49 +00006869 // (X << C1) >>u C2 --> X << (C1-C2) & (-1 >> C2)
Chris Lattner3e009e82007-02-05 00:57:54 +00006870 if (I.getOpcode() == Instruction::LShr) {
6871 assert(ShiftOp->getOpcode() == Instruction::Shl);
6872 Instruction *Shift =
6873 BinaryOperator::createShl(X, ConstantInt::get(Ty, ShiftDiff));
6874 InsertNewInstBefore(Shift, I);
6875
Reid Spencer441486c2007-03-26 23:45:51 +00006876 APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2));
Reid Spencer6274c722007-03-23 18:46:34 +00006877 return BinaryOperator::createAnd(Shift, ConstantInt::get(Mask));
Chris Lattner3e009e82007-02-05 00:57:54 +00006878 }
6879
6880 // We can't handle (X << C1) >>a C2, it shifts arbitrary bits in.
Chris Lattner86102b82005-01-01 16:22:27 +00006881 }
Chris Lattnereb372a02006-01-06 07:52:12 +00006882 }
Chris Lattnerf4cdbf32002-05-06 16:14:14 +00006883 return 0;
6884}
6885
Chris Lattner48a44f72002-05-02 17:06:02 +00006886
Chris Lattner8f663e82005-10-29 04:36:15 +00006887/// DecomposeSimpleLinearExpr - Analyze 'Val', seeing if it is a simple linear
6888/// expression. If so, decompose it, returning some value X, such that Val is
6889/// X*Scale+Offset.
6890///
6891static Value *DecomposeSimpleLinearExpr(Value *Val, unsigned &Scale,
Jeff Cohen5a1c7502007-04-04 16:58:57 +00006892 int &Offset) {
Reid Spencerc635f472006-12-31 05:48:39 +00006893 assert(Val->getType() == Type::Int32Ty && "Unexpected allocation size type!");
Reid Spencere0fc4df2006-10-20 07:07:24 +00006894 if (ConstantInt *CI = dyn_cast<ConstantInt>(Val)) {
Reid Spencerc635f472006-12-31 05:48:39 +00006895 Offset = CI->getZExtValue();
Chris Lattnerd8675e42007-10-12 05:30:59 +00006896 Scale = 0;
Reid Spencerc635f472006-12-31 05:48:39 +00006897 return ConstantInt::get(Type::Int32Ty, 0);
Chris Lattnerd8675e42007-10-12 05:30:59 +00006898 } else if (BinaryOperator *I = dyn_cast<BinaryOperator>(Val)) {
6899 if (ConstantInt *RHS = dyn_cast<ConstantInt>(I->getOperand(1))) {
6900 if (I->getOpcode() == Instruction::Shl) {
6901 // This is a value scaled by '1 << the shift amt'.
6902 Scale = 1U << RHS->getZExtValue();
6903 Offset = 0;
6904 return I->getOperand(0);
6905 } else if (I->getOpcode() == Instruction::Mul) {
6906 // This value is scaled by 'RHS'.
6907 Scale = RHS->getZExtValue();
6908 Offset = 0;
6909 return I->getOperand(0);
6910 } else if (I->getOpcode() == Instruction::Add) {
6911 // We have X+C. Check to see if we really have (X*C2)+C1,
6912 // where C1 is divisible by C2.
6913 unsigned SubScale;
6914 Value *SubVal =
6915 DecomposeSimpleLinearExpr(I->getOperand(0), SubScale, Offset);
6916 Offset += RHS->getZExtValue();
6917 Scale = SubScale;
6918 return SubVal;
Chris Lattner8f663e82005-10-29 04:36:15 +00006919 }
6920 }
6921 }
6922
6923 // Otherwise, we can't look past this.
6924 Scale = 1;
6925 Offset = 0;
6926 return Val;
6927}
6928
6929
Chris Lattner216be912005-10-24 06:03:58 +00006930/// PromoteCastOfAllocation - If we find a cast of an allocation instruction,
6931/// try to eliminate the cast by moving the type information into the alloc.
Chris Lattner1db224d2007-04-27 17:44:50 +00006932Instruction *InstCombiner::PromoteCastOfAllocation(BitCastInst &CI,
Chris Lattner216be912005-10-24 06:03:58 +00006933 AllocationInst &AI) {
Chris Lattner1db224d2007-04-27 17:44:50 +00006934 const PointerType *PTy = cast<PointerType>(CI.getType());
Chris Lattner216be912005-10-24 06:03:58 +00006935
Chris Lattnerac87beb2005-10-24 06:22:12 +00006936 // Remove any uses of AI that are dead.
6937 assert(!CI.use_empty() && "Dead instructions should be removed earlier!");
Chris Lattner99c6cf62007-02-15 22:52:10 +00006938
Chris Lattnerac87beb2005-10-24 06:22:12 +00006939 for (Value::use_iterator UI = AI.use_begin(), E = AI.use_end(); UI != E; ) {
6940 Instruction *User = cast<Instruction>(*UI++);
6941 if (isInstructionTriviallyDead(User)) {
6942 while (UI != E && *UI == User)
6943 ++UI; // If this instruction uses AI more than once, don't break UI.
6944
Chris Lattnerac87beb2005-10-24 06:22:12 +00006945 ++NumDeadInst;
Bill Wendling5dbf43c2006-11-26 09:46:52 +00006946 DOUT << "IC: DCE: " << *User;
Chris Lattner51f54572007-03-02 19:59:19 +00006947 EraseInstFromFunction(*User);
Chris Lattnerac87beb2005-10-24 06:22:12 +00006948 }
6949 }
6950
Chris Lattner216be912005-10-24 06:03:58 +00006951 // Get the type really allocated and the type casted to.
6952 const Type *AllocElTy = AI.getAllocatedType();
6953 const Type *CastElTy = PTy->getElementType();
6954 if (!AllocElTy->isSized() || !CastElTy->isSized()) return 0;
Chris Lattner355ecc02005-10-24 06:26:18 +00006955
Chris Lattner945e4372007-02-14 05:52:17 +00006956 unsigned AllocElTyAlign = TD->getABITypeAlignment(AllocElTy);
6957 unsigned CastElTyAlign = TD->getABITypeAlignment(CastElTy);
Chris Lattner355ecc02005-10-24 06:26:18 +00006958 if (CastElTyAlign < AllocElTyAlign) return 0;
6959
Chris Lattner46705b22005-10-24 06:35:18 +00006960 // If the allocation has multiple uses, only promote it if we are strictly
6961 // increasing the alignment of the resultant allocation. If we keep it the
6962 // same, we open the door to infinite loops of various kinds.
6963 if (!AI.hasOneUse() && CastElTyAlign == AllocElTyAlign) return 0;
6964
Duncan Sands44b87212007-11-01 20:53:16 +00006965 uint64_t AllocElTySize = TD->getABITypeSize(AllocElTy);
6966 uint64_t CastElTySize = TD->getABITypeSize(CastElTy);
Chris Lattnerbb171802005-10-27 05:53:56 +00006967 if (CastElTySize == 0 || AllocElTySize == 0) return 0;
Chris Lattner355ecc02005-10-24 06:26:18 +00006968
Chris Lattner8270c332005-10-29 03:19:53 +00006969 // See if we can satisfy the modulus by pulling a scale out of the array
6970 // size argument.
Jeff Cohen5a1c7502007-04-04 16:58:57 +00006971 unsigned ArraySizeScale;
6972 int ArrayOffset;
Chris Lattner8f663e82005-10-29 04:36:15 +00006973 Value *NumElements = // See if the array size is a decomposable linear expr.
6974 DecomposeSimpleLinearExpr(AI.getOperand(0), ArraySizeScale, ArrayOffset);
6975
Chris Lattner8270c332005-10-29 03:19:53 +00006976 // If we can now satisfy the modulus, by using a non-1 scale, we really can
6977 // do the xform.
Chris Lattner8f663e82005-10-29 04:36:15 +00006978 if ((AllocElTySize*ArraySizeScale) % CastElTySize != 0 ||
6979 (AllocElTySize*ArrayOffset ) % CastElTySize != 0) return 0;
Chris Lattnerb3ecf962005-10-27 06:12:00 +00006980
Chris Lattner8270c332005-10-29 03:19:53 +00006981 unsigned Scale = (AllocElTySize*ArraySizeScale)/CastElTySize;
6982 Value *Amt = 0;
6983 if (Scale == 1) {
6984 Amt = NumElements;
6985 } else {
Reid Spencere0fc4df2006-10-20 07:07:24 +00006986 // If the allocation size is constant, form a constant mul expression
Reid Spencerc635f472006-12-31 05:48:39 +00006987 Amt = ConstantInt::get(Type::Int32Ty, Scale);
6988 if (isa<ConstantInt>(NumElements))
Zhou Sheng9bc8ab12007-04-02 13:45:30 +00006989 Amt = Multiply(cast<ConstantInt>(NumElements), cast<ConstantInt>(Amt));
Reid Spencere0fc4df2006-10-20 07:07:24 +00006990 // otherwise multiply the amount and the number of elements
Chris Lattner8270c332005-10-29 03:19:53 +00006991 else if (Scale != 1) {
6992 Instruction *Tmp = BinaryOperator::createMul(Amt, NumElements, "tmp");
6993 Amt = InsertNewInstBefore(Tmp, AI);
Chris Lattnerb3ecf962005-10-27 06:12:00 +00006994 }
Chris Lattnerbb171802005-10-27 05:53:56 +00006995 }
6996
Jeff Cohen5a1c7502007-04-04 16:58:57 +00006997 if (int Offset = (AllocElTySize*ArrayOffset)/CastElTySize) {
6998 Value *Off = ConstantInt::get(Type::Int32Ty, Offset, true);
Chris Lattner8f663e82005-10-29 04:36:15 +00006999 Instruction *Tmp = BinaryOperator::createAdd(Amt, Off, "tmp");
7000 Amt = InsertNewInstBefore(Tmp, AI);
7001 }
7002
Chris Lattner216be912005-10-24 06:03:58 +00007003 AllocationInst *New;
7004 if (isa<MallocInst>(AI))
Chris Lattner6e0123b2007-02-11 01:23:03 +00007005 New = new MallocInst(CastElTy, Amt, AI.getAlignment());
Chris Lattner216be912005-10-24 06:03:58 +00007006 else
Chris Lattner6e0123b2007-02-11 01:23:03 +00007007 New = new AllocaInst(CastElTy, Amt, AI.getAlignment());
Chris Lattner216be912005-10-24 06:03:58 +00007008 InsertNewInstBefore(New, AI);
Chris Lattner6e0123b2007-02-11 01:23:03 +00007009 New->takeName(&AI);
Chris Lattner46705b22005-10-24 06:35:18 +00007010
7011 // If the allocation has multiple uses, insert a cast and change all things
7012 // that used it to use the new cast. This will also hack on CI, but it will
7013 // die soon.
7014 if (!AI.hasOneUse()) {
7015 AddUsesToWorkList(AI);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00007016 // New is the allocation instruction, pointer typed. AI is the original
7017 // allocation instruction, also pointer typed. Thus, cast to use is BitCast.
7018 CastInst *NewCast = new BitCastInst(New, AI.getType(), "tmpcast");
Chris Lattner46705b22005-10-24 06:35:18 +00007019 InsertNewInstBefore(NewCast, AI);
7020 AI.replaceAllUsesWith(NewCast);
7021 }
Chris Lattner216be912005-10-24 06:03:58 +00007022 return ReplaceInstUsesWith(CI, New);
7023}
7024
Chris Lattner1ebbe6a2006-05-13 02:06:03 +00007025/// CanEvaluateInDifferentType - Return true if we can take the specified value
Chris Lattnerda1d04a2007-03-03 05:27:34 +00007026/// and return it as type Ty without inserting any new casts and without
7027/// changing the computed value. This is used by code that tries to decide
7028/// whether promoting or shrinking integer operations to wider or smaller types
7029/// will allow us to eliminate a truncate or extend.
7030///
7031/// This is a truncation operation if Ty is smaller than V->getType(), or an
7032/// extension operation if Ty is larger.
Dan Gohman99b7b3f2008-04-10 18:43:06 +00007033bool InstCombiner::CanEvaluateInDifferentType(Value *V, const IntegerType *Ty,
7034 unsigned CastOpc,
7035 int &NumCastsRemoved) {
Chris Lattnerda1d04a2007-03-03 05:27:34 +00007036 // We can always evaluate constants in another type.
7037 if (isa<ConstantInt>(V))
7038 return true;
Chris Lattner1ebbe6a2006-05-13 02:06:03 +00007039
7040 Instruction *I = dyn_cast<Instruction>(V);
Chris Lattnerda1d04a2007-03-03 05:27:34 +00007041 if (!I) return false;
7042
7043 const IntegerType *OrigTy = cast<IntegerType>(V->getType());
Chris Lattner1ebbe6a2006-05-13 02:06:03 +00007044
Chris Lattnerb0418fc2007-08-02 06:11:14 +00007045 // If this is an extension or truncate, we can often eliminate it.
7046 if (isa<TruncInst>(I) || isa<ZExtInst>(I) || isa<SExtInst>(I)) {
7047 // If this is a cast from the destination type, we can trivially eliminate
7048 // it, and this will remove a cast overall.
7049 if (I->getOperand(0)->getType() == Ty) {
7050 // If the first operand is itself a cast, and is eliminable, do not count
7051 // this as an eliminable cast. We would prefer to eliminate those two
7052 // casts first.
7053 if (!isa<CastInst>(I->getOperand(0)))
7054 ++NumCastsRemoved;
7055 return true;
7056 }
7057 }
7058
7059 // We can't extend or shrink something that has multiple uses: doing so would
7060 // require duplicating the instruction in general, which isn't profitable.
7061 if (!I->hasOneUse()) return false;
7062
Chris Lattner1ebbe6a2006-05-13 02:06:03 +00007063 switch (I->getOpcode()) {
Chris Lattnerda1d04a2007-03-03 05:27:34 +00007064 case Instruction::Add:
7065 case Instruction::Sub:
Chris Lattner1ebbe6a2006-05-13 02:06:03 +00007066 case Instruction::And:
7067 case Instruction::Or:
7068 case Instruction::Xor:
7069 // These operators can all arbitrarily be extended or truncated.
Chris Lattnerb0418fc2007-08-02 06:11:14 +00007070 return CanEvaluateInDifferentType(I->getOperand(0), Ty, CastOpc,
7071 NumCastsRemoved) &&
7072 CanEvaluateInDifferentType(I->getOperand(1), Ty, CastOpc,
7073 NumCastsRemoved);
Chris Lattnerda1d04a2007-03-03 05:27:34 +00007074
Nick Lewycky78712e52008-01-22 05:08:48 +00007075 case Instruction::Mul:
Nick Lewycky78712e52008-01-22 05:08:48 +00007076 // A multiply can be truncated by truncating its operands.
7077 return Ty->getBitWidth() < OrigTy->getBitWidth() &&
7078 CanEvaluateInDifferentType(I->getOperand(0), Ty, CastOpc,
7079 NumCastsRemoved) &&
7080 CanEvaluateInDifferentType(I->getOperand(1), Ty, CastOpc,
7081 NumCastsRemoved);
7082
Chris Lattner960acb02006-11-29 07:18:39 +00007083 case Instruction::Shl:
Chris Lattnerda1d04a2007-03-03 05:27:34 +00007084 // If we are truncating the result of this SHL, and if it's a shift of a
7085 // constant amount, we can always perform a SHL in a smaller type.
7086 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
Zhou Shengfd28a332007-03-30 17:20:39 +00007087 uint32_t BitWidth = Ty->getBitWidth();
7088 if (BitWidth < OrigTy->getBitWidth() &&
7089 CI->getLimitedValue(BitWidth) < BitWidth)
Chris Lattnerb0418fc2007-08-02 06:11:14 +00007090 return CanEvaluateInDifferentType(I->getOperand(0), Ty, CastOpc,
7091 NumCastsRemoved);
Chris Lattnerda1d04a2007-03-03 05:27:34 +00007092 }
7093 break;
7094 case Instruction::LShr:
Chris Lattnerda1d04a2007-03-03 05:27:34 +00007095 // If this is a truncate of a logical shr, we can truncate it to a smaller
7096 // lshr iff we know that the bits we would otherwise be shifting in are
7097 // already zeros.
7098 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
Zhou Shengfd28a332007-03-30 17:20:39 +00007099 uint32_t OrigBitWidth = OrigTy->getBitWidth();
7100 uint32_t BitWidth = Ty->getBitWidth();
7101 if (BitWidth < OrigBitWidth &&
Chris Lattnerda1d04a2007-03-03 05:27:34 +00007102 MaskedValueIsZero(I->getOperand(0),
Zhou Shengfd28a332007-03-30 17:20:39 +00007103 APInt::getHighBitsSet(OrigBitWidth, OrigBitWidth-BitWidth)) &&
7104 CI->getLimitedValue(BitWidth) < BitWidth) {
Chris Lattnerb0418fc2007-08-02 06:11:14 +00007105 return CanEvaluateInDifferentType(I->getOperand(0), Ty, CastOpc,
7106 NumCastsRemoved);
Chris Lattnerda1d04a2007-03-03 05:27:34 +00007107 }
7108 }
Chris Lattner960acb02006-11-29 07:18:39 +00007109 break;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00007110 case Instruction::ZExt:
7111 case Instruction::SExt:
Chris Lattnerb0418fc2007-08-02 06:11:14 +00007112 case Instruction::Trunc:
7113 // If this is the same kind of case as our original (e.g. zext+zext), we
Chris Lattnerdc2cf222007-08-02 17:23:38 +00007114 // can safely replace it. Note that replacing it does not reduce the number
7115 // of casts in the input.
7116 if (I->getOpcode() == CastOpc)
Chris Lattner1ebbe6a2006-05-13 02:06:03 +00007117 return true;
Chris Lattnere8045672007-09-10 23:46:29 +00007118
Reid Spencer6c38f0b2006-11-27 01:05:10 +00007119 break;
7120 default:
Chris Lattner1ebbe6a2006-05-13 02:06:03 +00007121 // TODO: Can handle more cases here.
7122 break;
7123 }
7124
7125 return false;
7126}
7127
7128/// EvaluateInDifferentType - Given an expression that
7129/// CanEvaluateInDifferentType returns true for, actually insert the code to
7130/// evaluate the expression.
Reid Spencer74a528b2006-12-13 18:21:21 +00007131Value *InstCombiner::EvaluateInDifferentType(Value *V, const Type *Ty,
Chris Lattnerda1d04a2007-03-03 05:27:34 +00007132 bool isSigned) {
Chris Lattner1ebbe6a2006-05-13 02:06:03 +00007133 if (Constant *C = dyn_cast<Constant>(V))
Reid Spencer74a528b2006-12-13 18:21:21 +00007134 return ConstantExpr::getIntegerCast(C, Ty, isSigned /*Sext or ZExt*/);
Chris Lattner1ebbe6a2006-05-13 02:06:03 +00007135
7136 // Otherwise, it must be an instruction.
7137 Instruction *I = cast<Instruction>(V);
Chris Lattnerd0622b62006-05-20 23:14:03 +00007138 Instruction *Res = 0;
Chris Lattner1ebbe6a2006-05-13 02:06:03 +00007139 switch (I->getOpcode()) {
Chris Lattnerda1d04a2007-03-03 05:27:34 +00007140 case Instruction::Add:
7141 case Instruction::Sub:
Nick Lewycky78712e52008-01-22 05:08:48 +00007142 case Instruction::Mul:
Chris Lattner1ebbe6a2006-05-13 02:06:03 +00007143 case Instruction::And:
7144 case Instruction::Or:
Chris Lattnerda1d04a2007-03-03 05:27:34 +00007145 case Instruction::Xor:
Chris Lattner960acb02006-11-29 07:18:39 +00007146 case Instruction::AShr:
7147 case Instruction::LShr:
7148 case Instruction::Shl: {
Reid Spencer74a528b2006-12-13 18:21:21 +00007149 Value *LHS = EvaluateInDifferentType(I->getOperand(0), Ty, isSigned);
Chris Lattnerda1d04a2007-03-03 05:27:34 +00007150 Value *RHS = EvaluateInDifferentType(I->getOperand(1), Ty, isSigned);
7151 Res = BinaryOperator::create((Instruction::BinaryOps)I->getOpcode(),
7152 LHS, RHS, I->getName());
Chris Lattner960acb02006-11-29 07:18:39 +00007153 break;
7154 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00007155 case Instruction::Trunc:
7156 case Instruction::ZExt:
7157 case Instruction::SExt:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00007158 // If the source type of the cast is the type we're trying for then we can
Chris Lattnerb0418fc2007-08-02 06:11:14 +00007159 // just return the source. There's no need to insert it because it is not
7160 // new.
Chris Lattner1ebbe6a2006-05-13 02:06:03 +00007161 if (I->getOperand(0)->getType() == Ty)
7162 return I->getOperand(0);
7163
Chris Lattnerb0418fc2007-08-02 06:11:14 +00007164 // Otherwise, must be the same type of case, so just reinsert a new one.
7165 Res = CastInst::create(cast<CastInst>(I)->getOpcode(), I->getOperand(0),
7166 Ty, I->getName());
7167 break;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00007168 default:
Chris Lattner1ebbe6a2006-05-13 02:06:03 +00007169 // TODO: Can handle more cases here.
7170 assert(0 && "Unreachable!");
7171 break;
7172 }
7173
7174 return InsertNewInstBefore(Res, *I);
7175}
7176
Reid Spencer6c38f0b2006-11-27 01:05:10 +00007177/// @brief Implement the transforms common to all CastInst visitors.
7178Instruction *InstCombiner::commonCastTransforms(CastInst &CI) {
Chris Lattner55d4bda2003-06-23 21:59:52 +00007179 Value *Src = CI.getOperand(0);
7180
Dan Gohmanb5650eb2007-05-11 21:10:54 +00007181 // Many cases of "cast of a cast" are eliminable. If it's eliminable we just
Reid Spencer6c38f0b2006-11-27 01:05:10 +00007182 // eliminate it now.
Chris Lattner86102b82005-01-01 16:22:27 +00007183 if (CastInst *CSrc = dyn_cast<CastInst>(Src)) { // A->B->C cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00007184 if (Instruction::CastOps opc =
7185 isEliminableCastPair(CSrc, CI.getOpcode(), CI.getType(), TD)) {
7186 // The first cast (CSrc) is eliminable so we need to fix up or replace
7187 // the second cast (CI). CSrc will then have a good chance of being dead.
7188 return CastInst::create(opc, CSrc->getOperand(0), CI.getType());
Chris Lattner650b6da2002-08-02 20:00:25 +00007189 }
7190 }
Chris Lattner03841652004-05-25 04:29:21 +00007191
Reid Spencer6c38f0b2006-11-27 01:05:10 +00007192 // If we are casting a select then fold the cast into the select
Chris Lattner86102b82005-01-01 16:22:27 +00007193 if (SelectInst *SI = dyn_cast<SelectInst>(Src))
7194 if (Instruction *NV = FoldOpIntoSelect(CI, SI, this))
7195 return NV;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00007196
7197 // If we are casting a PHI then fold the cast into the PHI
Chris Lattner6a4adcd2004-09-29 05:07:12 +00007198 if (isa<PHINode>(Src))
7199 if (Instruction *NV = FoldOpIntoPhi(CI))
7200 return NV;
Chris Lattnerb19a5c62006-04-12 18:09:35 +00007201
Reid Spencer6c38f0b2006-11-27 01:05:10 +00007202 return 0;
7203}
7204
Chris Lattner1db224d2007-04-27 17:44:50 +00007205/// @brief Implement the transforms for cast of pointer (bitcast/ptrtoint)
7206Instruction *InstCombiner::commonPointerCastTransforms(CastInst &CI) {
7207 Value *Src = CI.getOperand(0);
7208
Chris Lattner1db224d2007-04-27 17:44:50 +00007209 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Src)) {
Chris Lattneracbf6a42007-04-28 00:57:34 +00007210 // If casting the result of a getelementptr instruction with no offset, turn
7211 // this into a cast of the original pointer!
Chris Lattner1db224d2007-04-27 17:44:50 +00007212 if (GEP->hasAllZeroIndices()) {
7213 // Changing the cast operand is usually not a good idea but it is safe
7214 // here because the pointer operand is being replaced with another
7215 // pointer operand so the opcode doesn't need to change.
Chris Lattneracbf6a42007-04-28 00:57:34 +00007216 AddToWorkList(GEP);
Chris Lattner1db224d2007-04-27 17:44:50 +00007217 CI.setOperand(0, GEP->getOperand(0));
7218 return &CI;
7219 }
Chris Lattneracbf6a42007-04-28 00:57:34 +00007220
7221 // If the GEP has a single use, and the base pointer is a bitcast, and the
7222 // GEP computes a constant offset, see if we can convert these three
7223 // instructions into fewer. This typically happens with unions and other
7224 // non-type-safe code.
7225 if (GEP->hasOneUse() && isa<BitCastInst>(GEP->getOperand(0))) {
7226 if (GEP->hasAllConstantIndices()) {
7227 // We are guaranteed to get a constant from EmitGEPOffset.
7228 ConstantInt *OffsetV = cast<ConstantInt>(EmitGEPOffset(GEP, CI, *this));
7229 int64_t Offset = OffsetV->getSExtValue();
7230
7231 // Get the base pointer input of the bitcast, and the type it points to.
7232 Value *OrigBase = cast<BitCastInst>(GEP->getOperand(0))->getOperand(0);
7233 const Type *GEPIdxTy =
7234 cast<PointerType>(OrigBase->getType())->getElementType();
7235 if (GEPIdxTy->isSized()) {
7236 SmallVector<Value*, 8> NewIndices;
7237
Chris Lattner5c827bd2007-05-05 01:59:31 +00007238 // Start with the index over the outer type. Note that the type size
7239 // might be zero (even if the offset isn't zero) if the indexed type
7240 // is something like [0 x {int, int}]
Chris Lattneracbf6a42007-04-28 00:57:34 +00007241 const Type *IntPtrTy = TD->getIntPtrType();
Chris Lattner5c827bd2007-05-05 01:59:31 +00007242 int64_t FirstIdx = 0;
Duncan Sands44b87212007-11-01 20:53:16 +00007243 if (int64_t TySize = TD->getABITypeSize(GEPIdxTy)) {
Chris Lattner5c827bd2007-05-05 01:59:31 +00007244 FirstIdx = Offset/TySize;
7245 Offset %= TySize;
Chris Lattneracbf6a42007-04-28 00:57:34 +00007246
Chris Lattner5c827bd2007-05-05 01:59:31 +00007247 // Handle silly modulus not returning values values [0..TySize).
7248 if (Offset < 0) {
7249 --FirstIdx;
7250 Offset += TySize;
7251 assert(Offset >= 0);
7252 }
Chris Lattner361e9812007-05-05 22:32:24 +00007253 assert((uint64_t)Offset < (uint64_t)TySize &&"Out of range offset");
Chris Lattneracbf6a42007-04-28 00:57:34 +00007254 }
7255
7256 NewIndices.push_back(ConstantInt::get(IntPtrTy, FirstIdx));
Chris Lattneracbf6a42007-04-28 00:57:34 +00007257
7258 // Index into the types. If we fail, set OrigBase to null.
7259 while (Offset) {
7260 if (const StructType *STy = dyn_cast<StructType>(GEPIdxTy)) {
7261 const StructLayout *SL = TD->getStructLayout(STy);
Chris Lattner234f96d2007-05-15 00:16:00 +00007262 if (Offset < (int64_t)SL->getSizeInBytes()) {
7263 unsigned Elt = SL->getElementContainingOffset(Offset);
7264 NewIndices.push_back(ConstantInt::get(Type::Int32Ty, Elt));
Chris Lattneracbf6a42007-04-28 00:57:34 +00007265
Chris Lattner234f96d2007-05-15 00:16:00 +00007266 Offset -= SL->getElementOffset(Elt);
7267 GEPIdxTy = STy->getElementType(Elt);
7268 } else {
7269 // Otherwise, we can't index into this, bail out.
7270 Offset = 0;
7271 OrigBase = 0;
7272 }
Chris Lattneracbf6a42007-04-28 00:57:34 +00007273 } else if (isa<ArrayType>(GEPIdxTy) || isa<VectorType>(GEPIdxTy)) {
7274 const SequentialType *STy = cast<SequentialType>(GEPIdxTy);
Duncan Sands44b87212007-11-01 20:53:16 +00007275 if (uint64_t EltSize = TD->getABITypeSize(STy->getElementType())){
Chris Lattner234f96d2007-05-15 00:16:00 +00007276 NewIndices.push_back(ConstantInt::get(IntPtrTy,Offset/EltSize));
7277 Offset %= EltSize;
7278 } else {
7279 NewIndices.push_back(ConstantInt::get(IntPtrTy, 0));
7280 }
Chris Lattneracbf6a42007-04-28 00:57:34 +00007281 GEPIdxTy = STy->getElementType();
7282 } else {
7283 // Otherwise, we can't index into this, bail out.
7284 Offset = 0;
7285 OrigBase = 0;
7286 }
7287 }
7288 if (OrigBase) {
7289 // If we were able to index down into an element, create the GEP
7290 // and bitcast the result. This eliminates one bitcast, potentially
7291 // two.
Gabor Greife9ecc682008-04-06 20:25:17 +00007292 Instruction *NGEP = GetElementPtrInst::Create(OrigBase,
7293 NewIndices.begin(),
7294 NewIndices.end(), "");
Chris Lattneracbf6a42007-04-28 00:57:34 +00007295 InsertNewInstBefore(NGEP, CI);
7296 NGEP->takeName(GEP);
7297
Chris Lattneracbf6a42007-04-28 00:57:34 +00007298 if (isa<BitCastInst>(CI))
7299 return new BitCastInst(NGEP, CI.getType());
7300 assert(isa<PtrToIntInst>(CI));
7301 return new PtrToIntInst(NGEP, CI.getType());
7302 }
7303 }
7304 }
7305 }
Chris Lattner1db224d2007-04-27 17:44:50 +00007306 }
7307
7308 return commonCastTransforms(CI);
7309}
7310
7311
7312
Chris Lattnerda1d04a2007-03-03 05:27:34 +00007313/// Only the TRUNC, ZEXT, SEXT, and BITCAST can both operand and result as
7314/// integer types. This function implements the common transforms for all those
Reid Spencer6c38f0b2006-11-27 01:05:10 +00007315/// cases.
7316/// @brief Implement the transforms common to CastInst with integer operands
7317Instruction *InstCombiner::commonIntCastTransforms(CastInst &CI) {
7318 if (Instruction *Result = commonCastTransforms(CI))
7319 return Result;
7320
7321 Value *Src = CI.getOperand(0);
7322 const Type *SrcTy = Src->getType();
7323 const Type *DestTy = CI.getType();
Zhou Sheng56cda952007-04-02 08:20:41 +00007324 uint32_t SrcBitSize = SrcTy->getPrimitiveSizeInBits();
7325 uint32_t DestBitSize = DestTy->getPrimitiveSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00007326
Reid Spencer6c38f0b2006-11-27 01:05:10 +00007327 // See if we can simplify any instructions used by the LHS whose sole
7328 // purpose is to compute bits we don't care about.
Reid Spencer4154e732007-03-22 20:56:53 +00007329 APInt KnownZero(DestBitSize, 0), KnownOne(DestBitSize, 0);
7330 if (SimplifyDemandedBits(&CI, APInt::getAllOnesValue(DestBitSize),
Reid Spencer6c38f0b2006-11-27 01:05:10 +00007331 KnownZero, KnownOne))
7332 return &CI;
7333
7334 // If the source isn't an instruction or has more than one use then we
7335 // can't do anything more.
Reid Spencer266e42b2006-12-23 06:05:41 +00007336 Instruction *SrcI = dyn_cast<Instruction>(Src);
7337 if (!SrcI || !Src->hasOneUse())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00007338 return 0;
7339
Chris Lattnerda1d04a2007-03-03 05:27:34 +00007340 // Attempt to propagate the cast into the instruction for int->int casts.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00007341 int NumCastsRemoved = 0;
Chris Lattnerda1d04a2007-03-03 05:27:34 +00007342 if (!isa<BitCastInst>(CI) &&
7343 CanEvaluateInDifferentType(SrcI, cast<IntegerType>(DestTy),
Chris Lattnerb0418fc2007-08-02 06:11:14 +00007344 CI.getOpcode(), NumCastsRemoved)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00007345 // If this cast is a truncate, evaluting in a different type always
Chris Lattnerb0418fc2007-08-02 06:11:14 +00007346 // eliminates the cast, so it is always a win. If this is a zero-extension,
7347 // we need to do an AND to maintain the clear top-part of the computation,
7348 // so we require that the input have eliminated at least one cast. If this
7349 // is a sign extension, we insert two new casts (to do the extension) so we
Reid Spencer6c38f0b2006-11-27 01:05:10 +00007350 // require that two casts have been eliminated.
Chris Lattnerda1d04a2007-03-03 05:27:34 +00007351 bool DoXForm;
7352 switch (CI.getOpcode()) {
7353 default:
7354 // All the others use floating point so we shouldn't actually
7355 // get here because of the check above.
7356 assert(0 && "Unknown cast type");
7357 case Instruction::Trunc:
7358 DoXForm = true;
7359 break;
7360 case Instruction::ZExt:
7361 DoXForm = NumCastsRemoved >= 1;
7362 break;
7363 case Instruction::SExt:
7364 DoXForm = NumCastsRemoved >= 2;
7365 break;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00007366 }
7367
7368 if (DoXForm) {
Reid Spencer74a528b2006-12-13 18:21:21 +00007369 Value *Res = EvaluateInDifferentType(SrcI, DestTy,
7370 CI.getOpcode() == Instruction::SExt);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00007371 assert(Res->getType() == DestTy);
7372 switch (CI.getOpcode()) {
7373 default: assert(0 && "Unknown cast type!");
7374 case Instruction::Trunc:
7375 case Instruction::BitCast:
7376 // Just replace this cast with the result.
7377 return ReplaceInstUsesWith(CI, Res);
7378 case Instruction::ZExt: {
7379 // We need to emit an AND to clear the high bits.
7380 assert(SrcBitSize < DestBitSize && "Not a zext?");
Chris Lattner9d5aace2007-04-02 05:48:58 +00007381 Constant *C = ConstantInt::get(APInt::getLowBitsSet(DestBitSize,
7382 SrcBitSize));
Reid Spencer6c38f0b2006-11-27 01:05:10 +00007383 return BinaryOperator::createAnd(Res, C);
7384 }
7385 case Instruction::SExt:
7386 // We need to emit a cast to truncate, then a cast to sext.
7387 return CastInst::create(Instruction::SExt,
Reid Spencer13bc5d72006-12-12 09:18:51 +00007388 InsertCastBefore(Instruction::Trunc, Res, Src->getType(),
7389 CI), DestTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00007390 }
7391 }
7392 }
7393
7394 Value *Op0 = SrcI->getNumOperands() > 0 ? SrcI->getOperand(0) : 0;
7395 Value *Op1 = SrcI->getNumOperands() > 1 ? SrcI->getOperand(1) : 0;
7396
7397 switch (SrcI->getOpcode()) {
7398 case Instruction::Add:
7399 case Instruction::Mul:
7400 case Instruction::And:
7401 case Instruction::Or:
7402 case Instruction::Xor:
Chris Lattnera74deaf2007-04-03 17:43:25 +00007403 // If we are discarding information, rewrite.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00007404 if (DestBitSize <= SrcBitSize && DestBitSize != 1) {
7405 // Don't insert two casts if they cannot be eliminated. We allow
7406 // two casts to be inserted if the sizes are the same. This could
7407 // only be converting signedness, which is a noop.
7408 if (DestBitSize == SrcBitSize ||
Reid Spencer266e42b2006-12-23 06:05:41 +00007409 !ValueRequiresCast(CI.getOpcode(), Op1, DestTy,TD) ||
7410 !ValueRequiresCast(CI.getOpcode(), Op0, DestTy, TD)) {
Reid Spencer2a499b02006-12-13 17:19:09 +00007411 Instruction::CastOps opcode = CI.getOpcode();
Reid Spencer13bc5d72006-12-12 09:18:51 +00007412 Value *Op0c = InsertOperandCastBefore(opcode, Op0, DestTy, SrcI);
7413 Value *Op1c = InsertOperandCastBefore(opcode, Op1, DestTy, SrcI);
7414 return BinaryOperator::create(
7415 cast<BinaryOperator>(SrcI)->getOpcode(), Op0c, Op1c);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00007416 }
7417 }
7418
7419 // cast (xor bool X, true) to int --> xor (cast bool X to int), 1
7420 if (isa<ZExtInst>(CI) && SrcBitSize == 1 &&
7421 SrcI->getOpcode() == Instruction::Xor &&
Zhou Sheng75b871f2007-01-11 12:24:14 +00007422 Op1 == ConstantInt::getTrue() &&
Reid Spencer266e42b2006-12-23 06:05:41 +00007423 (!Op0->hasOneUse() || !isa<CmpInst>(Op0))) {
Reid Spencer13bc5d72006-12-12 09:18:51 +00007424 Value *New = InsertOperandCastBefore(Instruction::ZExt, Op0, DestTy, &CI);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00007425 return BinaryOperator::createXor(New, ConstantInt::get(CI.getType(), 1));
7426 }
7427 break;
7428 case Instruction::SDiv:
7429 case Instruction::UDiv:
7430 case Instruction::SRem:
7431 case Instruction::URem:
7432 // If we are just changing the sign, rewrite.
7433 if (DestBitSize == SrcBitSize) {
7434 // Don't insert two casts if they cannot be eliminated. We allow
7435 // two casts to be inserted if the sizes are the same. This could
7436 // only be converting signedness, which is a noop.
Reid Spencer266e42b2006-12-23 06:05:41 +00007437 if (!ValueRequiresCast(CI.getOpcode(), Op1, DestTy, TD) ||
7438 !ValueRequiresCast(CI.getOpcode(), Op0, DestTy, TD)) {
Reid Spencer13bc5d72006-12-12 09:18:51 +00007439 Value *Op0c = InsertOperandCastBefore(Instruction::BitCast,
7440 Op0, DestTy, SrcI);
7441 Value *Op1c = InsertOperandCastBefore(Instruction::BitCast,
7442 Op1, DestTy, SrcI);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00007443 return BinaryOperator::create(
7444 cast<BinaryOperator>(SrcI)->getOpcode(), Op0c, Op1c);
7445 }
7446 }
7447 break;
7448
7449 case Instruction::Shl:
7450 // Allow changing the sign of the source operand. Do not allow
7451 // changing the size of the shift, UNLESS the shift amount is a
7452 // constant. We must not change variable sized shifts to a smaller
7453 // size, because it is undefined to shift more bits out than exist
7454 // in the value.
7455 if (DestBitSize == SrcBitSize ||
7456 (DestBitSize < SrcBitSize && isa<Constant>(Op1))) {
Reid Spencer13bc5d72006-12-12 09:18:51 +00007457 Instruction::CastOps opcode = (DestBitSize == SrcBitSize ?
7458 Instruction::BitCast : Instruction::Trunc);
7459 Value *Op0c = InsertOperandCastBefore(opcode, Op0, DestTy, SrcI);
Reid Spencer2341c222007-02-02 02:16:23 +00007460 Value *Op1c = InsertOperandCastBefore(opcode, Op1, DestTy, SrcI);
Reid Spencer0d5f9232007-02-02 14:08:20 +00007461 return BinaryOperator::createShl(Op0c, Op1c);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00007462 }
7463 break;
7464 case Instruction::AShr:
7465 // If this is a signed shr, and if all bits shifted in are about to be
7466 // truncated off, turn it into an unsigned shr to allow greater
7467 // simplifications.
7468 if (DestBitSize < SrcBitSize &&
7469 isa<ConstantInt>(Op1)) {
Zhou Shengfd28a332007-03-30 17:20:39 +00007470 uint32_t ShiftAmt = cast<ConstantInt>(Op1)->getLimitedValue(SrcBitSize);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00007471 if (SrcBitSize > ShiftAmt && SrcBitSize-ShiftAmt >= DestBitSize) {
7472 // Insert the new logical shift right.
Reid Spencer0d5f9232007-02-02 14:08:20 +00007473 return BinaryOperator::createLShr(Op0, Op1);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00007474 }
7475 }
7476 break;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00007477 }
7478 return 0;
7479}
7480
Chris Lattner74ff60f2007-04-11 06:57:46 +00007481Instruction *InstCombiner::visitTrunc(TruncInst &CI) {
Chris Lattnerd747f012006-11-29 07:04:07 +00007482 if (Instruction *Result = commonIntCastTransforms(CI))
7483 return Result;
7484
7485 Value *Src = CI.getOperand(0);
7486 const Type *Ty = CI.getType();
Zhou Sheng56cda952007-04-02 08:20:41 +00007487 uint32_t DestBitWidth = Ty->getPrimitiveSizeInBits();
7488 uint32_t SrcBitWidth = cast<IntegerType>(Src->getType())->getBitWidth();
Chris Lattnerd747f012006-11-29 07:04:07 +00007489
7490 if (Instruction *SrcI = dyn_cast<Instruction>(Src)) {
7491 switch (SrcI->getOpcode()) {
7492 default: break;
7493 case Instruction::LShr:
7494 // We can shrink lshr to something smaller if we know the bits shifted in
7495 // are already zeros.
7496 if (ConstantInt *ShAmtV = dyn_cast<ConstantInt>(SrcI->getOperand(1))) {
Zhou Shengfd28a332007-03-30 17:20:39 +00007497 uint32_t ShAmt = ShAmtV->getLimitedValue(SrcBitWidth);
Chris Lattnerd747f012006-11-29 07:04:07 +00007498
7499 // Get a mask for the bits shifting in.
Zhou Sheng2777a312007-03-28 09:19:01 +00007500 APInt Mask(APInt::getLowBitsSet(SrcBitWidth, ShAmt).shl(DestBitWidth));
Reid Spencer13bc5d72006-12-12 09:18:51 +00007501 Value* SrcIOp0 = SrcI->getOperand(0);
7502 if (SrcI->hasOneUse() && MaskedValueIsZero(SrcIOp0, Mask)) {
Chris Lattnerd747f012006-11-29 07:04:07 +00007503 if (ShAmt >= DestBitWidth) // All zeros.
7504 return ReplaceInstUsesWith(CI, Constant::getNullValue(Ty));
7505
7506 // Okay, we can shrink this. Truncate the input, then return a new
7507 // shift.
Reid Spencer2341c222007-02-02 02:16:23 +00007508 Value *V1 = InsertCastBefore(Instruction::Trunc, SrcIOp0, Ty, CI);
7509 Value *V2 = InsertCastBefore(Instruction::Trunc, SrcI->getOperand(1),
7510 Ty, CI);
Reid Spencer0d5f9232007-02-02 14:08:20 +00007511 return BinaryOperator::createLShr(V1, V2);
Chris Lattnerd747f012006-11-29 07:04:07 +00007512 }
Chris Lattnerc209b582006-12-05 01:26:29 +00007513 } else { // This is a variable shr.
7514
7515 // Turn 'trunc (lshr X, Y) to bool' into '(X & (1 << Y)) != 0'. This is
7516 // more LLVM instructions, but allows '1 << Y' to be hoisted if
7517 // loop-invariant and CSE'd.
Reid Spencer542964f2007-01-11 18:21:29 +00007518 if (CI.getType() == Type::Int1Ty && SrcI->hasOneUse()) {
Chris Lattnerc209b582006-12-05 01:26:29 +00007519 Value *One = ConstantInt::get(SrcI->getType(), 1);
7520
Reid Spencer2341c222007-02-02 02:16:23 +00007521 Value *V = InsertNewInstBefore(
Reid Spencer0d5f9232007-02-02 14:08:20 +00007522 BinaryOperator::createShl(One, SrcI->getOperand(1),
Reid Spencer2341c222007-02-02 02:16:23 +00007523 "tmp"), CI);
Chris Lattnerc209b582006-12-05 01:26:29 +00007524 V = InsertNewInstBefore(BinaryOperator::createAnd(V,
7525 SrcI->getOperand(0),
7526 "tmp"), CI);
7527 Value *Zero = Constant::getNullValue(V->getType());
Reid Spencer266e42b2006-12-23 06:05:41 +00007528 return new ICmpInst(ICmpInst::ICMP_NE, V, Zero);
Chris Lattnerc209b582006-12-05 01:26:29 +00007529 }
Chris Lattnerd747f012006-11-29 07:04:07 +00007530 }
7531 break;
7532 }
7533 }
7534
7535 return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00007536}
7537
Evan Chengc3cf9f82008-03-24 00:21:34 +00007538/// transformZExtICmp - Transform (zext icmp) to bitwise / integer operations
7539/// in order to eliminate the icmp.
7540Instruction *InstCombiner::transformZExtICmp(ICmpInst *ICI, Instruction &CI,
7541 bool DoXform) {
7542 // If we are just checking for a icmp eq of a single bit and zext'ing it
7543 // to an integer, then shift the bit to the appropriate place and then
7544 // cast to integer to avoid the comparison.
7545 if (ConstantInt *Op1C = dyn_cast<ConstantInt>(ICI->getOperand(1))) {
7546 const APInt &Op1CV = Op1C->getValue();
7547
7548 // zext (x <s 0) to i32 --> x>>u31 true if signbit set.
7549 // zext (x >s -1) to i32 --> (x>>u31)^1 true if signbit clear.
7550 if ((ICI->getPredicate() == ICmpInst::ICMP_SLT && Op1CV == 0) ||
7551 (ICI->getPredicate() == ICmpInst::ICMP_SGT &&Op1CV.isAllOnesValue())) {
7552 if (!DoXform) return ICI;
7553
7554 Value *In = ICI->getOperand(0);
7555 Value *Sh = ConstantInt::get(In->getType(),
7556 In->getType()->getPrimitiveSizeInBits()-1);
7557 In = InsertNewInstBefore(BinaryOperator::createLShr(In, Sh,
7558 In->getName()+".lobit"),
7559 CI);
7560 if (In->getType() != CI.getType())
7561 In = CastInst::createIntegerCast(In, CI.getType(),
7562 false/*ZExt*/, "tmp", &CI);
7563
7564 if (ICI->getPredicate() == ICmpInst::ICMP_SGT) {
7565 Constant *One = ConstantInt::get(In->getType(), 1);
7566 In = InsertNewInstBefore(BinaryOperator::createXor(In, One,
7567 In->getName()+".not"),
7568 CI);
7569 }
7570
7571 return ReplaceInstUsesWith(CI, In);
7572 }
7573
7574
7575
7576 // zext (X == 0) to i32 --> X^1 iff X has only the low bit set.
7577 // zext (X == 0) to i32 --> (X>>1)^1 iff X has only the 2nd bit set.
7578 // zext (X == 1) to i32 --> X iff X has only the low bit set.
7579 // zext (X == 2) to i32 --> X>>1 iff X has only the 2nd bit set.
7580 // zext (X != 0) to i32 --> X iff X has only the low bit set.
7581 // zext (X != 0) to i32 --> X>>1 iff X has only the 2nd bit set.
7582 // zext (X != 1) to i32 --> X^1 iff X has only the low bit set.
7583 // zext (X != 2) to i32 --> (X>>1)^1 iff X has only the 2nd bit set.
7584 if ((Op1CV == 0 || Op1CV.isPowerOf2()) &&
7585 // This only works for EQ and NE
7586 ICI->isEquality()) {
7587 // If Op1C some other power of two, convert:
7588 uint32_t BitWidth = Op1C->getType()->getBitWidth();
7589 APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0);
7590 APInt TypeMask(APInt::getAllOnesValue(BitWidth));
7591 ComputeMaskedBits(ICI->getOperand(0), TypeMask, KnownZero, KnownOne);
7592
7593 APInt KnownZeroMask(~KnownZero);
7594 if (KnownZeroMask.isPowerOf2()) { // Exactly 1 possible 1?
7595 if (!DoXform) return ICI;
7596
7597 bool isNE = ICI->getPredicate() == ICmpInst::ICMP_NE;
7598 if (Op1CV != 0 && (Op1CV != KnownZeroMask)) {
7599 // (X&4) == 2 --> false
7600 // (X&4) != 2 --> true
7601 Constant *Res = ConstantInt::get(Type::Int1Ty, isNE);
7602 Res = ConstantExpr::getZExt(Res, CI.getType());
7603 return ReplaceInstUsesWith(CI, Res);
7604 }
7605
7606 uint32_t ShiftAmt = KnownZeroMask.logBase2();
7607 Value *In = ICI->getOperand(0);
7608 if (ShiftAmt) {
7609 // Perform a logical shr by shiftamt.
7610 // Insert the shift to put the result in the low bit.
7611 In = InsertNewInstBefore(BinaryOperator::createLShr(In,
7612 ConstantInt::get(In->getType(), ShiftAmt),
7613 In->getName()+".lobit"), CI);
7614 }
7615
7616 if ((Op1CV != 0) == isNE) { // Toggle the low bit.
7617 Constant *One = ConstantInt::get(In->getType(), 1);
7618 In = BinaryOperator::createXor(In, One, "tmp");
7619 InsertNewInstBefore(cast<Instruction>(In), CI);
7620 }
7621
7622 if (CI.getType() == In->getType())
7623 return ReplaceInstUsesWith(CI, In);
7624 else
7625 return CastInst::createIntegerCast(In, CI.getType(), false/*ZExt*/);
7626 }
7627 }
7628 }
7629
7630 return 0;
7631}
7632
Chris Lattner74ff60f2007-04-11 06:57:46 +00007633Instruction *InstCombiner::visitZExt(ZExtInst &CI) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00007634 // If one of the common conversion will work ..
7635 if (Instruction *Result = commonIntCastTransforms(CI))
7636 return Result;
7637
7638 Value *Src = CI.getOperand(0);
7639
7640 // If this is a cast of a cast
7641 if (CastInst *CSrc = dyn_cast<CastInst>(Src)) { // A->B->C cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00007642 // If this is a TRUNC followed by a ZEXT then we are dealing with integral
7643 // types and if the sizes are just right we can convert this into a logical
7644 // 'and' which will be much cheaper than the pair of casts.
7645 if (isa<TruncInst>(CSrc)) {
7646 // Get the sizes of the types involved
7647 Value *A = CSrc->getOperand(0);
Zhou Sheng56cda952007-04-02 08:20:41 +00007648 uint32_t SrcSize = A->getType()->getPrimitiveSizeInBits();
7649 uint32_t MidSize = CSrc->getType()->getPrimitiveSizeInBits();
7650 uint32_t DstSize = CI.getType()->getPrimitiveSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00007651 // If we're actually extending zero bits and the trunc is a no-op
7652 if (MidSize < DstSize && SrcSize == DstSize) {
7653 // Replace both of the casts with an And of the type mask.
Zhou Sheng2777a312007-03-28 09:19:01 +00007654 APInt AndValue(APInt::getLowBitsSet(SrcSize, MidSize));
Reid Spencer4154e732007-03-22 20:56:53 +00007655 Constant *AndConst = ConstantInt::get(AndValue);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00007656 Instruction *And =
7657 BinaryOperator::createAnd(CSrc->getOperand(0), AndConst);
7658 // Unfortunately, if the type changed, we need to cast it back.
7659 if (And->getType() != CI.getType()) {
7660 And->setName(CSrc->getName()+".mask");
7661 InsertNewInstBefore(And, CI);
Reid Spencerbb65ebf2006-12-12 23:36:14 +00007662 And = CastInst::createIntegerCast(And, CI.getType(), false/*ZExt*/);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00007663 }
7664 return And;
7665 }
7666 }
7667 }
7668
Evan Chengc3cf9f82008-03-24 00:21:34 +00007669 if (ICmpInst *ICI = dyn_cast<ICmpInst>(Src))
7670 return transformZExtICmp(ICI, CI);
Chris Lattnerd0f79422007-04-11 06:53:04 +00007671
Evan Chengc3cf9f82008-03-24 00:21:34 +00007672 BinaryOperator *SrcI = dyn_cast<BinaryOperator>(Src);
7673 if (SrcI && SrcI->getOpcode() == Instruction::Or) {
7674 // zext (or icmp, icmp) --> or (zext icmp), (zext icmp) if at least one
7675 // of the (zext icmp) will be transformed.
7676 ICmpInst *LHS = dyn_cast<ICmpInst>(SrcI->getOperand(0));
7677 ICmpInst *RHS = dyn_cast<ICmpInst>(SrcI->getOperand(1));
7678 if (LHS && RHS && LHS->hasOneUse() && RHS->hasOneUse() &&
7679 (transformZExtICmp(LHS, CI, false) ||
7680 transformZExtICmp(RHS, CI, false))) {
7681 Value *LCast = InsertCastBefore(Instruction::ZExt, LHS, CI.getType(), CI);
7682 Value *RCast = InsertCastBefore(Instruction::ZExt, RHS, CI.getType(), CI);
7683 return BinaryOperator::create(Instruction::Or, LCast, RCast);
Chris Lattner7ddbff02007-04-11 05:45:39 +00007684 }
Evan Chengc3cf9f82008-03-24 00:21:34 +00007685 }
7686
Reid Spencer6c38f0b2006-11-27 01:05:10 +00007687 return 0;
7688}
7689
Chris Lattner74ff60f2007-04-11 06:57:46 +00007690Instruction *InstCombiner::visitSExt(SExtInst &CI) {
Chris Lattner20f23722007-04-11 06:12:58 +00007691 if (Instruction *I = commonIntCastTransforms(CI))
7692 return I;
7693
Chris Lattner74ff60f2007-04-11 06:57:46 +00007694 Value *Src = CI.getOperand(0);
7695
7696 // sext (x <s 0) -> ashr x, 31 -> all ones if signed
7697 // sext (x >s -1) -> ashr x, 31 -> all ones if not signed
7698 if (ICmpInst *ICI = dyn_cast<ICmpInst>(Src)) {
7699 // If we are just checking for a icmp eq of a single bit and zext'ing it
7700 // to an integer, then shift the bit to the appropriate place and then
7701 // cast to integer to avoid the comparison.
7702 if (ConstantInt *Op1C = dyn_cast<ConstantInt>(ICI->getOperand(1))) {
7703 const APInt &Op1CV = Op1C->getValue();
7704
7705 // sext (x <s 0) to i32 --> x>>s31 true if signbit set.
7706 // sext (x >s -1) to i32 --> (x>>s31)^-1 true if signbit clear.
7707 if ((ICI->getPredicate() == ICmpInst::ICMP_SLT && Op1CV == 0) ||
7708 (ICI->getPredicate() == ICmpInst::ICMP_SGT &&Op1CV.isAllOnesValue())){
7709 Value *In = ICI->getOperand(0);
7710 Value *Sh = ConstantInt::get(In->getType(),
7711 In->getType()->getPrimitiveSizeInBits()-1);
7712 In = InsertNewInstBefore(BinaryOperator::createAShr(In, Sh,
Chris Lattner28d921d2007-04-14 23:32:02 +00007713 In->getName()+".lobit"),
Chris Lattner74ff60f2007-04-11 06:57:46 +00007714 CI);
7715 if (In->getType() != CI.getType())
7716 In = CastInst::createIntegerCast(In, CI.getType(),
7717 true/*SExt*/, "tmp", &CI);
7718
7719 if (ICI->getPredicate() == ICmpInst::ICMP_SGT)
7720 In = InsertNewInstBefore(BinaryOperator::createNot(In,
7721 In->getName()+".not"), CI);
7722
7723 return ReplaceInstUsesWith(CI, In);
7724 }
7725 }
7726 }
7727
Chris Lattner20f23722007-04-11 06:12:58 +00007728 return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00007729}
7730
Chris Lattnerfa1e7ee2008-01-27 05:29:54 +00007731/// FitsInFPType - Return a Constant* for the specified FP constant if it fits
7732/// in the specified FP type without changing its value.
Chris Lattner3b187622008-04-20 00:41:09 +00007733static Constant *FitsInFPType(ConstantFP *CFP, const fltSemantics &Sem) {
Chris Lattnerfa1e7ee2008-01-27 05:29:54 +00007734 APFloat F = CFP->getValueAPF();
7735 if (F.convert(Sem, APFloat::rmNearestTiesToEven) == APFloat::opOK)
Chris Lattner3b187622008-04-20 00:41:09 +00007736 return ConstantFP::get(F);
Chris Lattnerfa1e7ee2008-01-27 05:29:54 +00007737 return 0;
7738}
7739
7740/// LookThroughFPExtensions - If this is an fp extension instruction, look
7741/// through it until we get the source value.
7742static Value *LookThroughFPExtensions(Value *V) {
7743 if (Instruction *I = dyn_cast<Instruction>(V))
7744 if (I->getOpcode() == Instruction::FPExt)
7745 return LookThroughFPExtensions(I->getOperand(0));
7746
7747 // If this value is a constant, return the constant in the smallest FP type
7748 // that can accurately represent it. This allows us to turn
7749 // (float)((double)X+2.0) into x+2.0f.
7750 if (ConstantFP *CFP = dyn_cast<ConstantFP>(V)) {
7751 if (CFP->getType() == Type::PPC_FP128Ty)
7752 return V; // No constant folding of this.
7753 // See if the value can be truncated to float and then reextended.
Chris Lattner3b187622008-04-20 00:41:09 +00007754 if (Value *V = FitsInFPType(CFP, APFloat::IEEEsingle))
Chris Lattnerfa1e7ee2008-01-27 05:29:54 +00007755 return V;
7756 if (CFP->getType() == Type::DoubleTy)
7757 return V; // Won't shrink.
Chris Lattner3b187622008-04-20 00:41:09 +00007758 if (Value *V = FitsInFPType(CFP, APFloat::IEEEdouble))
Chris Lattnerfa1e7ee2008-01-27 05:29:54 +00007759 return V;
7760 // Don't try to shrink to various long double types.
7761 }
7762
7763 return V;
7764}
7765
7766Instruction *InstCombiner::visitFPTrunc(FPTruncInst &CI) {
7767 if (Instruction *I = commonCastTransforms(CI))
7768 return I;
7769
7770 // If we have fptrunc(add (fpextend x), (fpextend y)), where x and y are
7771 // smaller than the destination type, we can eliminate the truncate by doing
7772 // the add as the smaller type. This applies to add/sub/mul/div as well as
7773 // many builtins (sqrt, etc).
7774 BinaryOperator *OpI = dyn_cast<BinaryOperator>(CI.getOperand(0));
7775 if (OpI && OpI->hasOneUse()) {
7776 switch (OpI->getOpcode()) {
7777 default: break;
7778 case Instruction::Add:
7779 case Instruction::Sub:
7780 case Instruction::Mul:
7781 case Instruction::FDiv:
7782 case Instruction::FRem:
7783 const Type *SrcTy = OpI->getType();
7784 Value *LHSTrunc = LookThroughFPExtensions(OpI->getOperand(0));
7785 Value *RHSTrunc = LookThroughFPExtensions(OpI->getOperand(1));
7786 if (LHSTrunc->getType() != SrcTy &&
7787 RHSTrunc->getType() != SrcTy) {
7788 unsigned DstSize = CI.getType()->getPrimitiveSizeInBits();
7789 // If the source types were both smaller than the destination type of
7790 // the cast, do this xform.
7791 if (LHSTrunc->getType()->getPrimitiveSizeInBits() <= DstSize &&
7792 RHSTrunc->getType()->getPrimitiveSizeInBits() <= DstSize) {
7793 LHSTrunc = InsertCastBefore(Instruction::FPExt, LHSTrunc,
7794 CI.getType(), CI);
7795 RHSTrunc = InsertCastBefore(Instruction::FPExt, RHSTrunc,
7796 CI.getType(), CI);
7797 return BinaryOperator::create(OpI->getOpcode(), LHSTrunc, RHSTrunc);
7798 }
7799 }
7800 break;
7801 }
7802 }
7803 return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00007804}
7805
7806Instruction *InstCombiner::visitFPExt(CastInst &CI) {
7807 return commonCastTransforms(CI);
7808}
7809
7810Instruction *InstCombiner::visitFPToUI(CastInst &CI) {
Reid Spencerad05ee92006-11-30 23:13:36 +00007811 return commonCastTransforms(CI);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00007812}
7813
7814Instruction *InstCombiner::visitFPToSI(CastInst &CI) {
Reid Spencerad05ee92006-11-30 23:13:36 +00007815 return commonCastTransforms(CI);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00007816}
7817
7818Instruction *InstCombiner::visitUIToFP(CastInst &CI) {
7819 return commonCastTransforms(CI);
7820}
7821
7822Instruction *InstCombiner::visitSIToFP(CastInst &CI) {
7823 return commonCastTransforms(CI);
7824}
7825
7826Instruction *InstCombiner::visitPtrToInt(CastInst &CI) {
Chris Lattner1db224d2007-04-27 17:44:50 +00007827 return commonPointerCastTransforms(CI);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00007828}
7829
Chris Lattner2940c5c2008-01-08 07:23:51 +00007830Instruction *InstCombiner::visitIntToPtr(IntToPtrInst &CI) {
7831 if (Instruction *I = commonCastTransforms(CI))
7832 return I;
7833
7834 const Type *DestPointee = cast<PointerType>(CI.getType())->getElementType();
7835 if (!DestPointee->isSized()) return 0;
7836
7837 // If this is inttoptr(add (ptrtoint x), cst), try to turn this into a GEP.
7838 ConstantInt *Cst;
7839 Value *X;
7840 if (match(CI.getOperand(0), m_Add(m_Cast<PtrToIntInst>(m_Value(X)),
7841 m_ConstantInt(Cst)))) {
7842 // If the source and destination operands have the same type, see if this
7843 // is a single-index GEP.
7844 if (X->getType() == CI.getType()) {
7845 // Get the size of the pointee type.
Bill Wendling68a930b2008-03-14 05:12:19 +00007846 uint64_t Size = TD->getABITypeSize(DestPointee);
Chris Lattner2940c5c2008-01-08 07:23:51 +00007847
7848 // Convert the constant to intptr type.
7849 APInt Offset = Cst->getValue();
7850 Offset.sextOrTrunc(TD->getPointerSizeInBits());
7851
7852 // If Offset is evenly divisible by Size, we can do this xform.
7853 if (Size && !APIntOps::srem(Offset, APInt(Offset.getBitWidth(), Size))){
7854 Offset = APIntOps::sdiv(Offset, APInt(Offset.getBitWidth(), Size));
Gabor Greife9ecc682008-04-06 20:25:17 +00007855 return GetElementPtrInst::Create(X, ConstantInt::get(Offset));
Chris Lattner2940c5c2008-01-08 07:23:51 +00007856 }
7857 }
7858 // TODO: Could handle other cases, e.g. where add is indexing into field of
7859 // struct etc.
7860 } else if (CI.getOperand(0)->hasOneUse() &&
7861 match(CI.getOperand(0), m_Add(m_Value(X), m_ConstantInt(Cst)))) {
7862 // Otherwise, if this is inttoptr(add x, cst), try to turn this into an
7863 // "inttoptr+GEP" instead of "add+intptr".
7864
7865 // Get the size of the pointee type.
7866 uint64_t Size = TD->getABITypeSize(DestPointee);
7867
7868 // Convert the constant to intptr type.
7869 APInt Offset = Cst->getValue();
7870 Offset.sextOrTrunc(TD->getPointerSizeInBits());
7871
7872 // If Offset is evenly divisible by Size, we can do this xform.
7873 if (Size && !APIntOps::srem(Offset, APInt(Offset.getBitWidth(), Size))){
7874 Offset = APIntOps::sdiv(Offset, APInt(Offset.getBitWidth(), Size));
7875
7876 Instruction *P = InsertNewInstBefore(new IntToPtrInst(X, CI.getType(),
7877 "tmp"), CI);
Gabor Greife9ecc682008-04-06 20:25:17 +00007878 return GetElementPtrInst::Create(P, ConstantInt::get(Offset), "tmp");
Chris Lattner2940c5c2008-01-08 07:23:51 +00007879 }
7880 }
7881 return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00007882}
7883
Chris Lattner1db224d2007-04-27 17:44:50 +00007884Instruction *InstCombiner::visitBitCast(BitCastInst &CI) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00007885 // If the operands are integer typed then apply the integer transforms,
7886 // otherwise just apply the common ones.
7887 Value *Src = CI.getOperand(0);
7888 const Type *SrcTy = Src->getType();
7889 const Type *DestTy = CI.getType();
7890
Chris Lattner03c49532007-01-15 02:27:26 +00007891 if (SrcTy->isInteger() && DestTy->isInteger()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00007892 if (Instruction *Result = commonIntCastTransforms(CI))
7893 return Result;
Chris Lattner1db224d2007-04-27 17:44:50 +00007894 } else if (isa<PointerType>(SrcTy)) {
7895 if (Instruction *I = commonPointerCastTransforms(CI))
7896 return I;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00007897 } else {
7898 if (Instruction *Result = commonCastTransforms(CI))
7899 return Result;
7900 }
7901
7902
7903 // Get rid of casts from one type to the same type. These are useless and can
7904 // be replaced by the operand.
7905 if (DestTy == Src->getType())
7906 return ReplaceInstUsesWith(CI, Src);
7907
Reid Spencer6c38f0b2006-11-27 01:05:10 +00007908 if (const PointerType *DstPTy = dyn_cast<PointerType>(DestTy)) {
Chris Lattner1db224d2007-04-27 17:44:50 +00007909 const PointerType *SrcPTy = cast<PointerType>(SrcTy);
7910 const Type *DstElTy = DstPTy->getElementType();
7911 const Type *SrcElTy = SrcPTy->getElementType();
7912
Nate Begemanf2b0b0e2008-03-31 00:22:16 +00007913 // If the address spaces don't match, don't eliminate the bitcast, which is
7914 // required for changing types.
7915 if (SrcPTy->getAddressSpace() != DstPTy->getAddressSpace())
7916 return 0;
7917
Chris Lattner1db224d2007-04-27 17:44:50 +00007918 // If we are casting a malloc or alloca to a pointer to a type of the same
7919 // size, rewrite the allocation instruction to allocate the "right" type.
7920 if (AllocationInst *AI = dyn_cast<AllocationInst>(Src))
7921 if (Instruction *V = PromoteCastOfAllocation(CI, *AI))
7922 return V;
7923
Chris Lattner361e9812007-05-05 22:32:24 +00007924 // If the source and destination are pointers, and this cast is equivalent
7925 // to a getelementptr X, 0, 0, 0... turn it into the appropriate gep.
Chris Lattner1db224d2007-04-27 17:44:50 +00007926 // This can enhance SROA and other transforms that want type-safe pointers.
7927 Constant *ZeroUInt = Constant::getNullValue(Type::Int32Ty);
7928 unsigned NumZeros = 0;
7929 while (SrcElTy != DstElTy &&
7930 isa<CompositeType>(SrcElTy) && !isa<PointerType>(SrcElTy) &&
7931 SrcElTy->getNumContainedTypes() /* not "{}" */) {
7932 SrcElTy = cast<CompositeType>(SrcElTy)->getTypeAtIndex(ZeroUInt);
7933 ++NumZeros;
7934 }
Chris Lattner6a4adcd2004-09-29 05:07:12 +00007935
Chris Lattner1db224d2007-04-27 17:44:50 +00007936 // If we found a path from the src to dest, create the getelementptr now.
7937 if (SrcElTy == DstElTy) {
7938 SmallVector<Value*, 8> Idxs(NumZeros+1, ZeroUInt);
Gabor Greife9ecc682008-04-06 20:25:17 +00007939 return GetElementPtrInst::Create(Src, Idxs.begin(), Idxs.end(), "",
7940 ((Instruction*) NULL));
Chris Lattnerb19a5c62006-04-12 18:09:35 +00007941 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00007942 }
Chris Lattnerdfae8be2003-07-24 17:35:25 +00007943
Reid Spencer6c38f0b2006-11-27 01:05:10 +00007944 if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(Src)) {
7945 if (SVI->hasOneUse()) {
7946 // Okay, we have (bitconvert (shuffle ..)). Check to see if this is
7947 // a bitconvert to a vector with the same # elts.
Reid Spencerd84d35b2007-02-15 02:26:10 +00007948 if (isa<VectorType>(DestTy) &&
7949 cast<VectorType>(DestTy)->getNumElements() ==
Reid Spencer6c38f0b2006-11-27 01:05:10 +00007950 SVI->getType()->getNumElements()) {
7951 CastInst *Tmp;
7952 // If either of the operands is a cast from CI.getType(), then
7953 // evaluating the shuffle in the casted destination's type will allow
7954 // us to eliminate at least one cast.
7955 if (((Tmp = dyn_cast<CastInst>(SVI->getOperand(0))) &&
7956 Tmp->getOperand(0)->getType() == DestTy) ||
7957 ((Tmp = dyn_cast<CastInst>(SVI->getOperand(1))) &&
7958 Tmp->getOperand(0)->getType() == DestTy)) {
Reid Spencer13bc5d72006-12-12 09:18:51 +00007959 Value *LHS = InsertOperandCastBefore(Instruction::BitCast,
7960 SVI->getOperand(0), DestTy, &CI);
7961 Value *RHS = InsertOperandCastBefore(Instruction::BitCast,
7962 SVI->getOperand(1), DestTy, &CI);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00007963 // Return a new shuffle vector. Use the same element ID's, as we
7964 // know the vector types match #elts.
7965 return new ShuffleVectorInst(LHS, RHS, SVI->getOperand(2));
Chris Lattner99155be2006-05-25 23:24:33 +00007966 }
7967 }
7968 }
7969 }
Chris Lattner260ab202002-04-18 17:39:14 +00007970 return 0;
Chris Lattnerca081252001-12-14 16:52:21 +00007971}
7972
Chris Lattner56e4d3d2004-04-09 23:46:01 +00007973/// GetSelectFoldableOperands - We want to turn code that looks like this:
7974/// %C = or %A, %B
7975/// %D = select %cond, %C, %A
7976/// into:
7977/// %C = select %cond, %B, 0
7978/// %D = or %A, %C
7979///
7980/// Assuming that the specified instruction is an operand to the select, return
7981/// a bitmask indicating which operands of this instruction are foldable if they
7982/// equal the other incoming value of the select.
7983///
7984static unsigned GetSelectFoldableOperands(Instruction *I) {
7985 switch (I->getOpcode()) {
7986 case Instruction::Add:
7987 case Instruction::Mul:
7988 case Instruction::And:
7989 case Instruction::Or:
7990 case Instruction::Xor:
7991 return 3; // Can fold through either operand.
7992 case Instruction::Sub: // Can only fold on the amount subtracted.
7993 case Instruction::Shl: // Can only fold on the shift amount.
Reid Spencerfdff9382006-11-08 06:47:33 +00007994 case Instruction::LShr:
7995 case Instruction::AShr:
Misha Brukmanb1c93172005-04-21 23:48:37 +00007996 return 1;
Chris Lattner56e4d3d2004-04-09 23:46:01 +00007997 default:
7998 return 0; // Cannot fold
7999 }
8000}
8001
8002/// GetSelectFoldableConstant - For the same transformation as the previous
8003/// function, return the identity constant that goes into the select.
8004static Constant *GetSelectFoldableConstant(Instruction *I) {
8005 switch (I->getOpcode()) {
8006 default: assert(0 && "This cannot happen!"); abort();
8007 case Instruction::Add:
8008 case Instruction::Sub:
8009 case Instruction::Or:
8010 case Instruction::Xor:
Chris Lattner56e4d3d2004-04-09 23:46:01 +00008011 case Instruction::Shl:
Reid Spencerfdff9382006-11-08 06:47:33 +00008012 case Instruction::LShr:
8013 case Instruction::AShr:
Reid Spencer2341c222007-02-02 02:16:23 +00008014 return Constant::getNullValue(I->getType());
Chris Lattner56e4d3d2004-04-09 23:46:01 +00008015 case Instruction::And:
Chris Lattner37338922007-06-15 06:23:19 +00008016 return Constant::getAllOnesValue(I->getType());
Chris Lattner56e4d3d2004-04-09 23:46:01 +00008017 case Instruction::Mul:
8018 return ConstantInt::get(I->getType(), 1);
8019 }
8020}
8021
Chris Lattner411336f2005-01-19 21:50:18 +00008022/// FoldSelectOpOp - Here we have (select c, TI, FI), and we know that TI and FI
8023/// have the same opcode and only one use each. Try to simplify this.
8024Instruction *InstCombiner::FoldSelectOpOp(SelectInst &SI, Instruction *TI,
8025 Instruction *FI) {
8026 if (TI->getNumOperands() == 1) {
8027 // If this is a non-volatile load or a cast from the same type,
8028 // merge.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00008029 if (TI->isCast()) {
Chris Lattner411336f2005-01-19 21:50:18 +00008030 if (TI->getOperand(0)->getType() != FI->getOperand(0)->getType())
8031 return 0;
8032 } else {
8033 return 0; // unknown unary op.
8034 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00008035
Chris Lattner411336f2005-01-19 21:50:18 +00008036 // Fold this by inserting a select from the input values.
Gabor Greife9ecc682008-04-06 20:25:17 +00008037 SelectInst *NewSI = SelectInst::Create(SI.getCondition(), TI->getOperand(0),
8038 FI->getOperand(0), SI.getName()+".v");
Chris Lattner411336f2005-01-19 21:50:18 +00008039 InsertNewInstBefore(NewSI, SI);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00008040 return CastInst::create(Instruction::CastOps(TI->getOpcode()), NewSI,
8041 TI->getType());
Chris Lattner411336f2005-01-19 21:50:18 +00008042 }
8043
Reid Spencer2341c222007-02-02 02:16:23 +00008044 // Only handle binary operators here.
8045 if (!isa<BinaryOperator>(TI))
Chris Lattner411336f2005-01-19 21:50:18 +00008046 return 0;
8047
8048 // Figure out if the operations have any operands in common.
8049 Value *MatchOp, *OtherOpT, *OtherOpF;
8050 bool MatchIsOpZero;
8051 if (TI->getOperand(0) == FI->getOperand(0)) {
8052 MatchOp = TI->getOperand(0);
8053 OtherOpT = TI->getOperand(1);
8054 OtherOpF = FI->getOperand(1);
8055 MatchIsOpZero = true;
8056 } else if (TI->getOperand(1) == FI->getOperand(1)) {
8057 MatchOp = TI->getOperand(1);
8058 OtherOpT = TI->getOperand(0);
8059 OtherOpF = FI->getOperand(0);
8060 MatchIsOpZero = false;
8061 } else if (!TI->isCommutative()) {
8062 return 0;
8063 } else if (TI->getOperand(0) == FI->getOperand(1)) {
8064 MatchOp = TI->getOperand(0);
8065 OtherOpT = TI->getOperand(1);
8066 OtherOpF = FI->getOperand(0);
8067 MatchIsOpZero = true;
8068 } else if (TI->getOperand(1) == FI->getOperand(0)) {
8069 MatchOp = TI->getOperand(1);
8070 OtherOpT = TI->getOperand(0);
8071 OtherOpF = FI->getOperand(1);
8072 MatchIsOpZero = true;
8073 } else {
8074 return 0;
8075 }
8076
8077 // If we reach here, they do have operations in common.
Gabor Greife9ecc682008-04-06 20:25:17 +00008078 SelectInst *NewSI = SelectInst::Create(SI.getCondition(), OtherOpT,
8079 OtherOpF, SI.getName()+".v");
Chris Lattner411336f2005-01-19 21:50:18 +00008080 InsertNewInstBefore(NewSI, SI);
8081
8082 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(TI)) {
8083 if (MatchIsOpZero)
8084 return BinaryOperator::create(BO->getOpcode(), MatchOp, NewSI);
8085 else
8086 return BinaryOperator::create(BO->getOpcode(), NewSI, MatchOp);
Chris Lattner411336f2005-01-19 21:50:18 +00008087 }
Reid Spencer2f34b982007-02-02 14:41:37 +00008088 assert(0 && "Shouldn't get here");
8089 return 0;
Chris Lattner411336f2005-01-19 21:50:18 +00008090}
8091
Chris Lattnerb909e8b2004-03-12 05:52:32 +00008092Instruction *InstCombiner::visitSelectInst(SelectInst &SI) {
Chris Lattner533bc492004-03-30 19:37:13 +00008093 Value *CondVal = SI.getCondition();
8094 Value *TrueVal = SI.getTrueValue();
8095 Value *FalseVal = SI.getFalseValue();
8096
8097 // select true, X, Y -> X
8098 // select false, X, Y -> Y
Zhou Sheng75b871f2007-01-11 12:24:14 +00008099 if (ConstantInt *C = dyn_cast<ConstantInt>(CondVal))
Reid Spencercddc9df2007-01-12 04:24:46 +00008100 return ReplaceInstUsesWith(SI, C->getZExtValue() ? TrueVal : FalseVal);
Chris Lattner533bc492004-03-30 19:37:13 +00008101
8102 // select C, X, X -> X
8103 if (TrueVal == FalseVal)
8104 return ReplaceInstUsesWith(SI, TrueVal);
8105
Chris Lattner81a7a232004-10-16 18:11:37 +00008106 if (isa<UndefValue>(TrueVal)) // select C, undef, X -> X
8107 return ReplaceInstUsesWith(SI, FalseVal);
8108 if (isa<UndefValue>(FalseVal)) // select C, X, undef -> X
8109 return ReplaceInstUsesWith(SI, TrueVal);
8110 if (isa<UndefValue>(CondVal)) { // select undef, X, Y -> X or Y
8111 if (isa<Constant>(TrueVal))
8112 return ReplaceInstUsesWith(SI, TrueVal);
8113 else
8114 return ReplaceInstUsesWith(SI, FalseVal);
8115 }
8116
Reid Spencer542964f2007-01-11 18:21:29 +00008117 if (SI.getType() == Type::Int1Ty) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +00008118 if (ConstantInt *C = dyn_cast<ConstantInt>(TrueVal)) {
Reid Spencercddc9df2007-01-12 04:24:46 +00008119 if (C->getZExtValue()) {
Chris Lattner1c631e82004-04-08 04:43:23 +00008120 // Change: A = select B, true, C --> A = or B, C
Chris Lattnerdf20a4d2004-06-10 02:07:29 +00008121 return BinaryOperator::createOr(CondVal, FalseVal);
Chris Lattner1c631e82004-04-08 04:43:23 +00008122 } else {
8123 // Change: A = select B, false, C --> A = and !B, C
8124 Value *NotCond =
8125 InsertNewInstBefore(BinaryOperator::createNot(CondVal,
8126 "not."+CondVal->getName()), SI);
Chris Lattnerdf20a4d2004-06-10 02:07:29 +00008127 return BinaryOperator::createAnd(NotCond, FalseVal);
Chris Lattner1c631e82004-04-08 04:43:23 +00008128 }
Reid Spencer7a9c62b2007-01-12 07:05:14 +00008129 } else if (ConstantInt *C = dyn_cast<ConstantInt>(FalseVal)) {
Reid Spencercddc9df2007-01-12 04:24:46 +00008130 if (C->getZExtValue() == false) {
Chris Lattner1c631e82004-04-08 04:43:23 +00008131 // Change: A = select B, C, false --> A = and B, C
Chris Lattnerdf20a4d2004-06-10 02:07:29 +00008132 return BinaryOperator::createAnd(CondVal, TrueVal);
Chris Lattner1c631e82004-04-08 04:43:23 +00008133 } else {
8134 // Change: A = select B, C, true --> A = or !B, C
8135 Value *NotCond =
8136 InsertNewInstBefore(BinaryOperator::createNot(CondVal,
8137 "not."+CondVal->getName()), SI);
Chris Lattnerdf20a4d2004-06-10 02:07:29 +00008138 return BinaryOperator::createOr(NotCond, TrueVal);
Chris Lattner1c631e82004-04-08 04:43:23 +00008139 }
8140 }
Chris Lattnerc00e8ad2007-11-25 21:27:53 +00008141
8142 // select a, b, a -> a&b
8143 // select a, a, b -> a|b
8144 if (CondVal == TrueVal)
8145 return BinaryOperator::createOr(CondVal, FalseVal);
8146 else if (CondVal == FalseVal)
8147 return BinaryOperator::createAnd(CondVal, TrueVal);
Zhou Sheng75b871f2007-01-11 12:24:14 +00008148 }
Chris Lattner1c631e82004-04-08 04:43:23 +00008149
Chris Lattner183b3362004-04-09 19:05:30 +00008150 // Selecting between two integer constants?
8151 if (ConstantInt *TrueValC = dyn_cast<ConstantInt>(TrueVal))
8152 if (ConstantInt *FalseValC = dyn_cast<ConstantInt>(FalseVal)) {
Chris Lattner20f23722007-04-11 06:12:58 +00008153 // select C, 1, 0 -> zext C to int
Reid Spencer959a21d2007-03-23 21:24:59 +00008154 if (FalseValC->isZero() && TrueValC->getValue() == 1) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00008155 return CastInst::create(Instruction::ZExt, CondVal, SI.getType());
Reid Spencer959a21d2007-03-23 21:24:59 +00008156 } else if (TrueValC->isZero() && FalseValC->getValue() == 1) {
Chris Lattner20f23722007-04-11 06:12:58 +00008157 // select C, 0, 1 -> zext !C to int
Chris Lattner183b3362004-04-09 19:05:30 +00008158 Value *NotCond =
8159 InsertNewInstBefore(BinaryOperator::createNot(CondVal,
Chris Lattnercf7baf32004-04-09 18:19:44 +00008160 "not."+CondVal->getName()), SI);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00008161 return CastInst::create(Instruction::ZExt, NotCond, SI.getType());
Chris Lattnercf7baf32004-04-09 18:19:44 +00008162 }
Chris Lattner20f23722007-04-11 06:12:58 +00008163
8164 // FIXME: Turn select 0/-1 and -1/0 into sext from condition!
Chris Lattner35167c32004-06-09 07:59:58 +00008165
Reid Spencer266e42b2006-12-23 06:05:41 +00008166 if (ICmpInst *IC = dyn_cast<ICmpInst>(SI.getCondition())) {
Chris Lattner380c7e92006-09-20 04:44:59 +00008167
Reid Spencer266e42b2006-12-23 06:05:41 +00008168 // (x <s 0) ? -1 : 0 -> ashr x, 31
Reid Spencer959a21d2007-03-23 21:24:59 +00008169 if (TrueValC->isAllOnesValue() && FalseValC->isZero())
Chris Lattner380c7e92006-09-20 04:44:59 +00008170 if (ConstantInt *CmpCst = dyn_cast<ConstantInt>(IC->getOperand(1))) {
Chris Lattner20f23722007-04-11 06:12:58 +00008171 if (IC->getPredicate() == ICmpInst::ICMP_SLT && CmpCst->isZero()) {
Chris Lattner380c7e92006-09-20 04:44:59 +00008172 // The comparison constant and the result are not neccessarily the
Reid Spencer6c38f0b2006-11-27 01:05:10 +00008173 // same width. Make an all-ones value by inserting a AShr.
Chris Lattner380c7e92006-09-20 04:44:59 +00008174 Value *X = IC->getOperand(0);
Zhou Sheng56cda952007-04-02 08:20:41 +00008175 uint32_t Bits = X->getType()->getPrimitiveSizeInBits();
Reid Spencer2341c222007-02-02 02:16:23 +00008176 Constant *ShAmt = ConstantInt::get(X->getType(), Bits-1);
8177 Instruction *SRA = BinaryOperator::create(Instruction::AShr, X,
8178 ShAmt, "ones");
Chris Lattner380c7e92006-09-20 04:44:59 +00008179 InsertNewInstBefore(SRA, SI);
8180
Reid Spencer6c38f0b2006-11-27 01:05:10 +00008181 // Finally, convert to the type of the select RHS. We figure out
8182 // if this requires a SExt, Trunc or BitCast based on the sizes.
8183 Instruction::CastOps opc = Instruction::BitCast;
Zhou Sheng56cda952007-04-02 08:20:41 +00008184 uint32_t SRASize = SRA->getType()->getPrimitiveSizeInBits();
8185 uint32_t SISize = SI.getType()->getPrimitiveSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00008186 if (SRASize < SISize)
8187 opc = Instruction::SExt;
8188 else if (SRASize > SISize)
8189 opc = Instruction::Trunc;
8190 return CastInst::create(opc, SRA, SI.getType());
Chris Lattner380c7e92006-09-20 04:44:59 +00008191 }
8192 }
8193
8194
8195 // If one of the constants is zero (we know they can't both be) and we
Chris Lattner20f23722007-04-11 06:12:58 +00008196 // have an icmp instruction with zero, and we have an 'and' with the
Chris Lattner380c7e92006-09-20 04:44:59 +00008197 // non-constant value, eliminate this whole mess. This corresponds to
8198 // cases like this: ((X & 27) ? 27 : 0)
Reid Spencer959a21d2007-03-23 21:24:59 +00008199 if (TrueValC->isZero() || FalseValC->isZero())
Chris Lattnerb3f24c92006-09-18 04:22:48 +00008200 if (IC->isEquality() && isa<ConstantInt>(IC->getOperand(1)) &&
Chris Lattner35167c32004-06-09 07:59:58 +00008201 cast<Constant>(IC->getOperand(1))->isNullValue())
8202 if (Instruction *ICA = dyn_cast<Instruction>(IC->getOperand(0)))
8203 if (ICA->getOpcode() == Instruction::And &&
Misha Brukmanb1c93172005-04-21 23:48:37 +00008204 isa<ConstantInt>(ICA->getOperand(1)) &&
8205 (ICA->getOperand(1) == TrueValC ||
8206 ICA->getOperand(1) == FalseValC) &&
Chris Lattner35167c32004-06-09 07:59:58 +00008207 isOneBitSet(cast<ConstantInt>(ICA->getOperand(1)))) {
8208 // Okay, now we know that everything is set up, we just don't
Reid Spencer266e42b2006-12-23 06:05:41 +00008209 // know whether we have a icmp_ne or icmp_eq and whether the
8210 // true or false val is the zero.
Reid Spencer959a21d2007-03-23 21:24:59 +00008211 bool ShouldNotVal = !TrueValC->isZero();
Reid Spencer266e42b2006-12-23 06:05:41 +00008212 ShouldNotVal ^= IC->getPredicate() == ICmpInst::ICMP_NE;
Chris Lattner35167c32004-06-09 07:59:58 +00008213 Value *V = ICA;
8214 if (ShouldNotVal)
8215 V = InsertNewInstBefore(BinaryOperator::create(
8216 Instruction::Xor, V, ICA->getOperand(1)), SI);
8217 return ReplaceInstUsesWith(SI, V);
8218 }
Chris Lattner380c7e92006-09-20 04:44:59 +00008219 }
Chris Lattner533bc492004-03-30 19:37:13 +00008220 }
Chris Lattner623fba12004-04-10 22:21:27 +00008221
8222 // See if we are selecting two values based on a comparison of the two values.
Reid Spencer266e42b2006-12-23 06:05:41 +00008223 if (FCmpInst *FCI = dyn_cast<FCmpInst>(CondVal)) {
8224 if (FCI->getOperand(0) == TrueVal && FCI->getOperand(1) == FalseVal) {
Chris Lattner623fba12004-04-10 22:21:27 +00008225 // Transform (X == Y) ? X : Y -> Y
Dale Johannesen9d559cf2007-10-03 17:45:27 +00008226 if (FCI->getPredicate() == FCmpInst::FCMP_OEQ) {
8227 // This is not safe in general for floating point:
8228 // consider X== -0, Y== +0.
8229 // It becomes safe if either operand is a nonzero constant.
8230 ConstantFP *CFPt, *CFPf;
8231 if (((CFPt = dyn_cast<ConstantFP>(TrueVal)) &&
8232 !CFPt->getValueAPF().isZero()) ||
8233 ((CFPf = dyn_cast<ConstantFP>(FalseVal)) &&
8234 !CFPf->getValueAPF().isZero()))
Chris Lattner623fba12004-04-10 22:21:27 +00008235 return ReplaceInstUsesWith(SI, FalseVal);
Dale Johannesen9d559cf2007-10-03 17:45:27 +00008236 }
Chris Lattner623fba12004-04-10 22:21:27 +00008237 // Transform (X != Y) ? X : Y -> X
Reid Spencer266e42b2006-12-23 06:05:41 +00008238 if (FCI->getPredicate() == FCmpInst::FCMP_ONE)
Chris Lattner623fba12004-04-10 22:21:27 +00008239 return ReplaceInstUsesWith(SI, TrueVal);
8240 // NOTE: if we wanted to, this is where to detect MIN/MAX/ABS/etc.
8241
Reid Spencer266e42b2006-12-23 06:05:41 +00008242 } else if (FCI->getOperand(0) == FalseVal && FCI->getOperand(1) == TrueVal){
Chris Lattner623fba12004-04-10 22:21:27 +00008243 // Transform (X == Y) ? Y : X -> X
Dale Johannesen9d559cf2007-10-03 17:45:27 +00008244 if (FCI->getPredicate() == FCmpInst::FCMP_OEQ) {
8245 // This is not safe in general for floating point:
8246 // consider X== -0, Y== +0.
8247 // It becomes safe if either operand is a nonzero constant.
8248 ConstantFP *CFPt, *CFPf;
8249 if (((CFPt = dyn_cast<ConstantFP>(TrueVal)) &&
8250 !CFPt->getValueAPF().isZero()) ||
8251 ((CFPf = dyn_cast<ConstantFP>(FalseVal)) &&
8252 !CFPf->getValueAPF().isZero()))
8253 return ReplaceInstUsesWith(SI, FalseVal);
8254 }
Chris Lattner623fba12004-04-10 22:21:27 +00008255 // Transform (X != Y) ? Y : X -> Y
Reid Spencer266e42b2006-12-23 06:05:41 +00008256 if (FCI->getPredicate() == FCmpInst::FCMP_ONE)
8257 return ReplaceInstUsesWith(SI, TrueVal);
8258 // NOTE: if we wanted to, this is where to detect MIN/MAX/ABS/etc.
8259 }
8260 }
8261
8262 // See if we are selecting two values based on a comparison of the two values.
8263 if (ICmpInst *ICI = dyn_cast<ICmpInst>(CondVal)) {
8264 if (ICI->getOperand(0) == TrueVal && ICI->getOperand(1) == FalseVal) {
8265 // Transform (X == Y) ? X : Y -> Y
8266 if (ICI->getPredicate() == ICmpInst::ICMP_EQ)
8267 return ReplaceInstUsesWith(SI, FalseVal);
8268 // Transform (X != Y) ? X : Y -> X
8269 if (ICI->getPredicate() == ICmpInst::ICMP_NE)
8270 return ReplaceInstUsesWith(SI, TrueVal);
8271 // NOTE: if we wanted to, this is where to detect MIN/MAX/ABS/etc.
8272
8273 } else if (ICI->getOperand(0) == FalseVal && ICI->getOperand(1) == TrueVal){
8274 // Transform (X == Y) ? Y : X -> X
8275 if (ICI->getPredicate() == ICmpInst::ICMP_EQ)
8276 return ReplaceInstUsesWith(SI, FalseVal);
8277 // Transform (X != Y) ? Y : X -> Y
8278 if (ICI->getPredicate() == ICmpInst::ICMP_NE)
Chris Lattner24cf0202004-04-11 01:39:19 +00008279 return ReplaceInstUsesWith(SI, TrueVal);
Chris Lattner623fba12004-04-10 22:21:27 +00008280 // NOTE: if we wanted to, this is where to detect MIN/MAX/ABS/etc.
8281 }
8282 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00008283
Chris Lattnera04c9042005-01-13 22:52:24 +00008284 if (Instruction *TI = dyn_cast<Instruction>(TrueVal))
8285 if (Instruction *FI = dyn_cast<Instruction>(FalseVal))
8286 if (TI->hasOneUse() && FI->hasOneUse()) {
Chris Lattnera04c9042005-01-13 22:52:24 +00008287 Instruction *AddOp = 0, *SubOp = 0;
8288
Chris Lattner411336f2005-01-19 21:50:18 +00008289 // Turn (select C, (op X, Y), (op X, Z)) -> (op X, (select C, Y, Z))
8290 if (TI->getOpcode() == FI->getOpcode())
8291 if (Instruction *IV = FoldSelectOpOp(SI, TI, FI))
8292 return IV;
8293
8294 // Turn select C, (X+Y), (X-Y) --> (X+(select C, Y, (-Y))). This is
8295 // even legal for FP.
Chris Lattnera04c9042005-01-13 22:52:24 +00008296 if (TI->getOpcode() == Instruction::Sub &&
8297 FI->getOpcode() == Instruction::Add) {
8298 AddOp = FI; SubOp = TI;
8299 } else if (FI->getOpcode() == Instruction::Sub &&
8300 TI->getOpcode() == Instruction::Add) {
8301 AddOp = TI; SubOp = FI;
8302 }
8303
8304 if (AddOp) {
8305 Value *OtherAddOp = 0;
8306 if (SubOp->getOperand(0) == AddOp->getOperand(0)) {
8307 OtherAddOp = AddOp->getOperand(1);
8308 } else if (SubOp->getOperand(0) == AddOp->getOperand(1)) {
8309 OtherAddOp = AddOp->getOperand(0);
8310 }
8311
8312 if (OtherAddOp) {
Chris Lattnerb580d262006-02-24 18:05:58 +00008313 // So at this point we know we have (Y -> OtherAddOp):
8314 // select C, (add X, Y), (sub X, Z)
8315 Value *NegVal; // Compute -Z
8316 if (Constant *C = dyn_cast<Constant>(SubOp->getOperand(1))) {
8317 NegVal = ConstantExpr::getNeg(C);
8318 } else {
8319 NegVal = InsertNewInstBefore(
8320 BinaryOperator::createNeg(SubOp->getOperand(1), "tmp"), SI);
Chris Lattnera04c9042005-01-13 22:52:24 +00008321 }
Chris Lattnerb580d262006-02-24 18:05:58 +00008322
8323 Value *NewTrueOp = OtherAddOp;
8324 Value *NewFalseOp = NegVal;
8325 if (AddOp != TI)
8326 std::swap(NewTrueOp, NewFalseOp);
8327 Instruction *NewSel =
Gabor Greife9ecc682008-04-06 20:25:17 +00008328 SelectInst::Create(CondVal, NewTrueOp,NewFalseOp,SI.getName()+".p");
Chris Lattnerb580d262006-02-24 18:05:58 +00008329
8330 NewSel = InsertNewInstBefore(NewSel, SI);
8331 return BinaryOperator::createAdd(SubOp->getOperand(0), NewSel);
Chris Lattnera04c9042005-01-13 22:52:24 +00008332 }
8333 }
8334 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00008335
Chris Lattner56e4d3d2004-04-09 23:46:01 +00008336 // See if we can fold the select into one of our operands.
Chris Lattner03c49532007-01-15 02:27:26 +00008337 if (SI.getType()->isInteger()) {
Chris Lattner56e4d3d2004-04-09 23:46:01 +00008338 // See the comment above GetSelectFoldableOperands for a description of the
8339 // transformation we are doing here.
8340 if (Instruction *TVI = dyn_cast<Instruction>(TrueVal))
8341 if (TVI->hasOneUse() && TVI->getNumOperands() == 2 &&
8342 !isa<Constant>(FalseVal))
8343 if (unsigned SFO = GetSelectFoldableOperands(TVI)) {
8344 unsigned OpToFold = 0;
8345 if ((SFO & 1) && FalseVal == TVI->getOperand(0)) {
8346 OpToFold = 1;
8347 } else if ((SFO & 2) && FalseVal == TVI->getOperand(1)) {
8348 OpToFold = 2;
8349 }
8350
8351 if (OpToFold) {
8352 Constant *C = GetSelectFoldableConstant(TVI);
Chris Lattner56e4d3d2004-04-09 23:46:01 +00008353 Instruction *NewSel =
Gabor Greife9ecc682008-04-06 20:25:17 +00008354 SelectInst::Create(SI.getCondition(), TVI->getOperand(2-OpToFold), C);
Chris Lattner56e4d3d2004-04-09 23:46:01 +00008355 InsertNewInstBefore(NewSel, SI);
Chris Lattner6e0123b2007-02-11 01:23:03 +00008356 NewSel->takeName(TVI);
Chris Lattner56e4d3d2004-04-09 23:46:01 +00008357 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(TVI))
8358 return BinaryOperator::create(BO->getOpcode(), FalseVal, NewSel);
Chris Lattner56e4d3d2004-04-09 23:46:01 +00008359 else {
8360 assert(0 && "Unknown instruction!!");
8361 }
8362 }
8363 }
Chris Lattner6862fbd2004-09-29 17:40:11 +00008364
Chris Lattner56e4d3d2004-04-09 23:46:01 +00008365 if (Instruction *FVI = dyn_cast<Instruction>(FalseVal))
8366 if (FVI->hasOneUse() && FVI->getNumOperands() == 2 &&
8367 !isa<Constant>(TrueVal))
8368 if (unsigned SFO = GetSelectFoldableOperands(FVI)) {
8369 unsigned OpToFold = 0;
8370 if ((SFO & 1) && TrueVal == FVI->getOperand(0)) {
8371 OpToFold = 1;
8372 } else if ((SFO & 2) && TrueVal == FVI->getOperand(1)) {
8373 OpToFold = 2;
8374 }
8375
8376 if (OpToFold) {
8377 Constant *C = GetSelectFoldableConstant(FVI);
Chris Lattner56e4d3d2004-04-09 23:46:01 +00008378 Instruction *NewSel =
Gabor Greife9ecc682008-04-06 20:25:17 +00008379 SelectInst::Create(SI.getCondition(), C, FVI->getOperand(2-OpToFold));
Chris Lattner56e4d3d2004-04-09 23:46:01 +00008380 InsertNewInstBefore(NewSel, SI);
Chris Lattner6e0123b2007-02-11 01:23:03 +00008381 NewSel->takeName(FVI);
Chris Lattner56e4d3d2004-04-09 23:46:01 +00008382 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(FVI))
8383 return BinaryOperator::create(BO->getOpcode(), TrueVal, NewSel);
Reid Spencer2341c222007-02-02 02:16:23 +00008384 else
Chris Lattner56e4d3d2004-04-09 23:46:01 +00008385 assert(0 && "Unknown instruction!!");
Chris Lattner56e4d3d2004-04-09 23:46:01 +00008386 }
8387 }
8388 }
Chris Lattnerd6f636a2005-04-24 07:30:14 +00008389
8390 if (BinaryOperator::isNot(CondVal)) {
8391 SI.setOperand(0, BinaryOperator::getNotArgument(CondVal));
8392 SI.setOperand(1, FalseVal);
8393 SI.setOperand(2, TrueVal);
8394 return &SI;
8395 }
8396
Chris Lattnerb909e8b2004-03-12 05:52:32 +00008397 return 0;
8398}
8399
Dan Gohman99b7b3f2008-04-10 18:43:06 +00008400/// EnforceKnownAlignment - If the specified pointer points to an object that
8401/// we control, modify the object's alignment to PrefAlign. This isn't
8402/// often possible though. If alignment is important, a more reliable approach
8403/// is to simply align all global variables and allocation instructions to
8404/// their preferred alignment from the beginning.
8405///
8406static unsigned EnforceKnownAlignment(Value *V,
8407 unsigned Align, unsigned PrefAlign) {
Chris Lattnera8e4b4b2007-08-09 19:05:49 +00008408
Dan Gohman99b7b3f2008-04-10 18:43:06 +00008409 User *U = dyn_cast<User>(V);
8410 if (!U) return Align;
8411
8412 switch (getOpcode(U)) {
8413 default: break;
8414 case Instruction::BitCast:
8415 return EnforceKnownAlignment(U->getOperand(0), Align, PrefAlign);
8416 case Instruction::GetElementPtr: {
Chris Lattner82f2ef22006-03-06 20:18:44 +00008417 // If all indexes are zero, it is just the alignment of the base pointer.
8418 bool AllZeroOperands = true;
Dan Gohman99b7b3f2008-04-10 18:43:06 +00008419 for (unsigned i = 1, e = U->getNumOperands(); i != e; ++i)
8420 if (!isa<Constant>(U->getOperand(i)) ||
8421 !cast<Constant>(U->getOperand(i))->isNullValue()) {
Chris Lattner82f2ef22006-03-06 20:18:44 +00008422 AllZeroOperands = false;
8423 break;
8424 }
Chris Lattnera8e4b4b2007-08-09 19:05:49 +00008425
8426 if (AllZeroOperands) {
8427 // Treat this like a bitcast.
Dan Gohman99b7b3f2008-04-10 18:43:06 +00008428 return EnforceKnownAlignment(U->getOperand(0), Align, PrefAlign);
Chris Lattnera8e4b4b2007-08-09 19:05:49 +00008429 }
Dan Gohman99b7b3f2008-04-10 18:43:06 +00008430 break;
Chris Lattner82f2ef22006-03-06 20:18:44 +00008431 }
Dan Gohman99b7b3f2008-04-10 18:43:06 +00008432 }
8433
8434 if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
8435 // If there is a large requested alignment and we can, bump up the alignment
8436 // of the global.
8437 if (!GV->isDeclaration()) {
8438 GV->setAlignment(PrefAlign);
8439 Align = PrefAlign;
8440 }
8441 } else if (AllocationInst *AI = dyn_cast<AllocationInst>(V)) {
8442 // If there is a requested alignment and if this is an alloca, round up. We
8443 // don't do this for malloc, because some systems can't respect the request.
8444 if (isa<AllocaInst>(AI)) {
8445 AI->setAlignment(PrefAlign);
8446 Align = PrefAlign;
8447 }
8448 }
8449
8450 return Align;
8451}
8452
8453/// GetOrEnforceKnownAlignment - If the specified pointer has an alignment that
8454/// we can determine, return it, otherwise return 0. If PrefAlign is specified,
8455/// and it is more than the alignment of the ultimate object, see if we can
8456/// increase the alignment of the ultimate object, making this check succeed.
8457unsigned InstCombiner::GetOrEnforceKnownAlignment(Value *V,
8458 unsigned PrefAlign) {
8459 unsigned BitWidth = TD ? TD->getTypeSizeInBits(V->getType()) :
8460 sizeof(PrefAlign) * CHAR_BIT;
8461 APInt Mask = APInt::getAllOnesValue(BitWidth);
8462 APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0);
8463 ComputeMaskedBits(V, Mask, KnownZero, KnownOne);
8464 unsigned TrailZ = KnownZero.countTrailingOnes();
8465 unsigned Align = 1u << std::min(BitWidth - 1, TrailZ);
8466
8467 if (PrefAlign > Align)
8468 Align = EnforceKnownAlignment(V, Align, PrefAlign);
8469
8470 // We don't need to make any adjustment.
8471 return Align;
Chris Lattner82f2ef22006-03-06 20:18:44 +00008472}
8473
Chris Lattner57974c82008-01-13 23:50:23 +00008474Instruction *InstCombiner::SimplifyMemTransfer(MemIntrinsic *MI) {
Dan Gohman99b7b3f2008-04-10 18:43:06 +00008475 unsigned DstAlign = GetOrEnforceKnownAlignment(MI->getOperand(1));
8476 unsigned SrcAlign = GetOrEnforceKnownAlignment(MI->getOperand(2));
Chris Lattner57974c82008-01-13 23:50:23 +00008477 unsigned MinAlign = std::min(DstAlign, SrcAlign);
8478 unsigned CopyAlign = MI->getAlignment()->getZExtValue();
8479
8480 if (CopyAlign < MinAlign) {
8481 MI->setAlignment(ConstantInt::get(Type::Int32Ty, MinAlign));
8482 return MI;
8483 }
8484
8485 // If MemCpyInst length is 1/2/4/8 bytes then replace memcpy with
8486 // load/store.
8487 ConstantInt *MemOpLength = dyn_cast<ConstantInt>(MI->getOperand(3));
8488 if (MemOpLength == 0) return 0;
8489
Chris Lattner92bd7852008-01-14 00:28:35 +00008490 // Source and destination pointer types are always "i8*" for intrinsic. See
8491 // if the size is something we can handle with a single primitive load/store.
8492 // A single load+store correctly handles overlapping memory in the memmove
8493 // case.
Chris Lattner57974c82008-01-13 23:50:23 +00008494 unsigned Size = MemOpLength->getZExtValue();
Chris Lattner2dc44262008-04-30 06:39:11 +00008495 if (Size == 0) return MI; // Delete this mem transfer.
8496
8497 if (Size > 8 || (Size&(Size-1)))
Chris Lattner92bd7852008-01-14 00:28:35 +00008498 return 0; // If not 1/2/4/8 bytes, exit.
Chris Lattner57974c82008-01-13 23:50:23 +00008499
Chris Lattner92bd7852008-01-14 00:28:35 +00008500 // Use an integer load+store unless we can find something better.
Chris Lattner57974c82008-01-13 23:50:23 +00008501 Type *NewPtrTy = PointerType::getUnqual(IntegerType::get(Size<<3));
Chris Lattner92bd7852008-01-14 00:28:35 +00008502
8503 // Memcpy forces the use of i8* for the source and destination. That means
8504 // that if you're using memcpy to move one double around, you'll get a cast
8505 // from double* to i8*. We'd much rather use a double load+store rather than
8506 // an i64 load+store, here because this improves the odds that the source or
8507 // dest address will be promotable. See if we can find a better type than the
8508 // integer datatype.
8509 if (Value *Op = getBitCastOperand(MI->getOperand(1))) {
8510 const Type *SrcETy = cast<PointerType>(Op->getType())->getElementType();
8511 if (SrcETy->isSized() && TD->getTypeStoreSize(SrcETy) == Size) {
8512 // The SrcETy might be something like {{{double}}} or [1 x double]. Rip
8513 // down through these levels if so.
8514 while (!SrcETy->isFirstClassType()) {
8515 if (const StructType *STy = dyn_cast<StructType>(SrcETy)) {
8516 if (STy->getNumElements() == 1)
8517 SrcETy = STy->getElementType(0);
8518 else
8519 break;
8520 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(SrcETy)) {
8521 if (ATy->getNumElements() == 1)
8522 SrcETy = ATy->getElementType();
8523 else
8524 break;
8525 } else
8526 break;
8527 }
8528
8529 if (SrcETy->isFirstClassType())
8530 NewPtrTy = PointerType::getUnqual(SrcETy);
8531 }
8532 }
8533
8534
Chris Lattner57974c82008-01-13 23:50:23 +00008535 // If the memcpy/memmove provides better alignment info than we can
8536 // infer, use it.
8537 SrcAlign = std::max(SrcAlign, CopyAlign);
8538 DstAlign = std::max(DstAlign, CopyAlign);
8539
8540 Value *Src = InsertBitCastBefore(MI->getOperand(2), NewPtrTy, *MI);
8541 Value *Dest = InsertBitCastBefore(MI->getOperand(1), NewPtrTy, *MI);
Chris Lattner92bd7852008-01-14 00:28:35 +00008542 Instruction *L = new LoadInst(Src, "tmp", false, SrcAlign);
8543 InsertNewInstBefore(L, *MI);
8544 InsertNewInstBefore(new StoreInst(L, Dest, false, DstAlign), *MI);
8545
8546 // Set the size of the copy to 0, it will be deleted on the next iteration.
8547 MI->setOperand(3, Constant::getNullValue(MemOpLength->getType()));
8548 return MI;
Chris Lattner57974c82008-01-13 23:50:23 +00008549}
Chris Lattnerb909e8b2004-03-12 05:52:32 +00008550
Chris Lattner2dc44262008-04-30 06:39:11 +00008551Instruction *InstCombiner::SimplifyMemSet(MemSetInst *MI) {
8552 unsigned Alignment = GetOrEnforceKnownAlignment(MI->getDest());
8553 if (MI->getAlignment()->getZExtValue() < Alignment) {
8554 MI->setAlignment(ConstantInt::get(Type::Int32Ty, Alignment));
8555 return MI;
8556 }
8557
8558 // Extract the length and alignment and fill if they are constant.
8559 ConstantInt *LenC = dyn_cast<ConstantInt>(MI->getLength());
8560 ConstantInt *FillC = dyn_cast<ConstantInt>(MI->getValue());
8561 if (!LenC || !FillC || FillC->getType() != Type::Int8Ty)
8562 return 0;
8563 uint64_t Len = LenC->getZExtValue();
8564 Alignment = MI->getAlignment()->getZExtValue();
8565
8566 // If the length is zero, this is a no-op
8567 if (Len == 0) return MI; // memset(d,c,0,a) -> noop
8568
8569 // memset(s,c,n) -> store s, c (for n=1,2,4,8)
8570 if (Len <= 8 && isPowerOf2_32((uint32_t)Len)) {
8571 const Type *ITy = IntegerType::get(Len*8); // n=1 -> i8.
8572
8573 Value *Dest = MI->getDest();
8574 Dest = InsertBitCastBefore(Dest, PointerType::getUnqual(ITy), *MI);
8575
8576 // Alignment 0 is identity for alignment 1 for memset, but not store.
8577 if (Alignment == 0) Alignment = 1;
8578
8579 // Extract the fill value and store.
8580 uint64_t Fill = FillC->getZExtValue()*0x0101010101010101ULL;
8581 InsertNewInstBefore(new StoreInst(ConstantInt::get(ITy, Fill), Dest, false,
8582 Alignment), *MI);
8583
8584 // Set the size of the copy to 0, it will be deleted on the next iteration.
8585 MI->setLength(Constant::getNullValue(LenC->getType()));
8586 return MI;
8587 }
8588
8589 return 0;
8590}
8591
8592
Chris Lattnerc66b2232006-01-13 20:11:04 +00008593/// visitCallInst - CallInst simplification. This mostly only handles folding
8594/// of intrinsic instructions. For normal calls, it allows visitCallSite to do
8595/// the heavy lifting.
8596///
Chris Lattner970c33a2003-06-19 17:00:31 +00008597Instruction *InstCombiner::visitCallInst(CallInst &CI) {
Chris Lattnerc66b2232006-01-13 20:11:04 +00008598 IntrinsicInst *II = dyn_cast<IntrinsicInst>(&CI);
8599 if (!II) return visitCallSite(&CI);
8600
Chris Lattner51ea1272004-02-28 05:22:00 +00008601 // Intrinsics cannot occur in an invoke, so handle them here instead of in
8602 // visitCallSite.
Chris Lattnerc66b2232006-01-13 20:11:04 +00008603 if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(II)) {
Chris Lattner00648e12004-10-12 04:52:52 +00008604 bool Changed = false;
8605
8606 // memmove/cpy/set of zero bytes is a noop.
8607 if (Constant *NumBytes = dyn_cast<Constant>(MI->getLength())) {
8608 if (NumBytes->isNullValue()) return EraseInstFromFunction(CI);
8609
Chris Lattner00648e12004-10-12 04:52:52 +00008610 if (ConstantInt *CI = dyn_cast<ConstantInt>(NumBytes))
Reid Spencere0fc4df2006-10-20 07:07:24 +00008611 if (CI->getZExtValue() == 1) {
Chris Lattner00648e12004-10-12 04:52:52 +00008612 // Replace the instruction with just byte operations. We would
8613 // transform other cases to loads/stores, but we don't know if
8614 // alignment is sufficient.
8615 }
Chris Lattner51ea1272004-02-28 05:22:00 +00008616 }
8617
Chris Lattner00648e12004-10-12 04:52:52 +00008618 // If we have a memmove and the source operation is a constant global,
8619 // then the source and dest pointers can't alias, so we can change this
8620 // into a call to memcpy.
Chris Lattner57974c82008-01-13 23:50:23 +00008621 if (MemMoveInst *MMI = dyn_cast<MemMoveInst>(MI)) {
Chris Lattner00648e12004-10-12 04:52:52 +00008622 if (GlobalVariable *GVSrc = dyn_cast<GlobalVariable>(MMI->getSource()))
8623 if (GVSrc->isConstant()) {
8624 Module *M = CI.getParent()->getParent()->getParent();
Chris Lattner5a866122008-01-13 22:23:22 +00008625 Intrinsic::ID MemCpyID;
8626 if (CI.getOperand(3)->getType() == Type::Int32Ty)
8627 MemCpyID = Intrinsic::memcpy_i32;
Chris Lattner681ef2f2006-03-03 01:34:17 +00008628 else
Chris Lattner5a866122008-01-13 22:23:22 +00008629 MemCpyID = Intrinsic::memcpy_i64;
8630 CI.setOperand(0, Intrinsic::getDeclaration(M, MemCpyID));
Chris Lattner00648e12004-10-12 04:52:52 +00008631 Changed = true;
8632 }
Chris Lattner82f2ef22006-03-06 20:18:44 +00008633 }
Chris Lattner00648e12004-10-12 04:52:52 +00008634
Chris Lattner82f2ef22006-03-06 20:18:44 +00008635 // If we can determine a pointer alignment that is bigger than currently
8636 // set, update the alignment.
8637 if (isa<MemCpyInst>(MI) || isa<MemMoveInst>(MI)) {
Chris Lattner57974c82008-01-13 23:50:23 +00008638 if (Instruction *I = SimplifyMemTransfer(MI))
8639 return I;
Chris Lattner2dc44262008-04-30 06:39:11 +00008640 } else if (MemSetInst *MSI = dyn_cast<MemSetInst>(MI)) {
8641 if (Instruction *I = SimplifyMemSet(MSI))
8642 return I;
Chris Lattner82f2ef22006-03-06 20:18:44 +00008643 }
8644
Chris Lattnerc66b2232006-01-13 20:11:04 +00008645 if (Changed) return II;
Chris Lattner503221f2006-01-13 21:28:09 +00008646 } else {
8647 switch (II->getIntrinsicID()) {
8648 default: break;
Chris Lattnerf42d0ae2006-04-02 05:30:25 +00008649 case Intrinsic::ppc_altivec_lvx:
8650 case Intrinsic::ppc_altivec_lvxl:
Chris Lattner36dd7c92006-04-17 22:26:56 +00008651 case Intrinsic::x86_sse_loadu_ps:
8652 case Intrinsic::x86_sse2_loadu_pd:
8653 case Intrinsic::x86_sse2_loadu_dq:
8654 // Turn PPC lvx -> load if the pointer is known aligned.
8655 // Turn X86 loadups -> load if the pointer is known aligned.
Dan Gohman99b7b3f2008-04-10 18:43:06 +00008656 if (GetOrEnforceKnownAlignment(II->getOperand(1), 16) >= 16) {
Chris Lattner5a866122008-01-13 22:23:22 +00008657 Value *Ptr = InsertBitCastBefore(II->getOperand(1),
8658 PointerType::getUnqual(II->getType()),
8659 CI);
Chris Lattnerf42d0ae2006-04-02 05:30:25 +00008660 return new LoadInst(Ptr);
8661 }
8662 break;
8663 case Intrinsic::ppc_altivec_stvx:
8664 case Intrinsic::ppc_altivec_stvxl:
8665 // Turn stvx -> store if the pointer is known aligned.
Dan Gohman99b7b3f2008-04-10 18:43:06 +00008666 if (GetOrEnforceKnownAlignment(II->getOperand(2), 16) >= 16) {
Christopher Lambedf07882007-12-17 01:12:55 +00008667 const Type *OpPtrTy =
8668 PointerType::getUnqual(II->getOperand(1)->getType());
Chris Lattner5a866122008-01-13 22:23:22 +00008669 Value *Ptr = InsertBitCastBefore(II->getOperand(2), OpPtrTy, CI);
Chris Lattnerf42d0ae2006-04-02 05:30:25 +00008670 return new StoreInst(II->getOperand(1), Ptr);
8671 }
8672 break;
Chris Lattner36dd7c92006-04-17 22:26:56 +00008673 case Intrinsic::x86_sse_storeu_ps:
8674 case Intrinsic::x86_sse2_storeu_pd:
8675 case Intrinsic::x86_sse2_storeu_dq:
8676 case Intrinsic::x86_sse2_storel_dq:
8677 // Turn X86 storeu -> store if the pointer is known aligned.
Dan Gohman99b7b3f2008-04-10 18:43:06 +00008678 if (GetOrEnforceKnownAlignment(II->getOperand(1), 16) >= 16) {
Christopher Lambedf07882007-12-17 01:12:55 +00008679 const Type *OpPtrTy =
8680 PointerType::getUnqual(II->getOperand(2)->getType());
Chris Lattner5a866122008-01-13 22:23:22 +00008681 Value *Ptr = InsertBitCastBefore(II->getOperand(1), OpPtrTy, CI);
Chris Lattner36dd7c92006-04-17 22:26:56 +00008682 return new StoreInst(II->getOperand(2), Ptr);
8683 }
8684 break;
Chris Lattner2deeaea2006-10-05 06:55:50 +00008685
8686 case Intrinsic::x86_sse_cvttss2si: {
8687 // These intrinsics only demands the 0th element of its input vector. If
8688 // we can simplify the input based on that, do so now.
8689 uint64_t UndefElts;
8690 if (Value *V = SimplifyDemandedVectorElts(II->getOperand(1), 1,
8691 UndefElts)) {
8692 II->setOperand(1, V);
8693 return II;
8694 }
8695 break;
8696 }
8697
Chris Lattnere79d2492006-04-06 19:19:17 +00008698 case Intrinsic::ppc_altivec_vperm:
8699 // Turn vperm(V1,V2,mask) -> shuffle(V1,V2,mask) if mask is a constant.
Reid Spencerd84d35b2007-02-15 02:26:10 +00008700 if (ConstantVector *Mask = dyn_cast<ConstantVector>(II->getOperand(3))) {
Chris Lattnere79d2492006-04-06 19:19:17 +00008701 assert(Mask->getNumOperands() == 16 && "Bad type for intrinsic!");
8702
8703 // Check that all of the elements are integer constants or undefs.
8704 bool AllEltsOk = true;
8705 for (unsigned i = 0; i != 16; ++i) {
8706 if (!isa<ConstantInt>(Mask->getOperand(i)) &&
8707 !isa<UndefValue>(Mask->getOperand(i))) {
8708 AllEltsOk = false;
8709 break;
8710 }
8711 }
8712
8713 if (AllEltsOk) {
8714 // Cast the input vectors to byte vectors.
Chris Lattner5a866122008-01-13 22:23:22 +00008715 Value *Op0 =InsertBitCastBefore(II->getOperand(1),Mask->getType(),CI);
8716 Value *Op1 =InsertBitCastBefore(II->getOperand(2),Mask->getType(),CI);
Chris Lattnere79d2492006-04-06 19:19:17 +00008717 Value *Result = UndefValue::get(Op0->getType());
8718
8719 // Only extract each element once.
8720 Value *ExtractedElts[32];
8721 memset(ExtractedElts, 0, sizeof(ExtractedElts));
8722
8723 for (unsigned i = 0; i != 16; ++i) {
8724 if (isa<UndefValue>(Mask->getOperand(i)))
8725 continue;
Chris Lattner28d921d2007-04-14 23:32:02 +00008726 unsigned Idx=cast<ConstantInt>(Mask->getOperand(i))->getZExtValue();
Chris Lattnere79d2492006-04-06 19:19:17 +00008727 Idx &= 31; // Match the hardware behavior.
8728
8729 if (ExtractedElts[Idx] == 0) {
8730 Instruction *Elt =
Chris Lattner2deeaea2006-10-05 06:55:50 +00008731 new ExtractElementInst(Idx < 16 ? Op0 : Op1, Idx&15, "tmp");
Chris Lattnere79d2492006-04-06 19:19:17 +00008732 InsertNewInstBefore(Elt, CI);
8733 ExtractedElts[Idx] = Elt;
8734 }
8735
8736 // Insert this value into the result vector.
Gabor Greife9ecc682008-04-06 20:25:17 +00008737 Result = InsertElementInst::Create(Result, ExtractedElts[Idx], i, "tmp");
Chris Lattnere79d2492006-04-06 19:19:17 +00008738 InsertNewInstBefore(cast<Instruction>(Result), CI);
8739 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00008740 return CastInst::create(Instruction::BitCast, Result, CI.getType());
Chris Lattnere79d2492006-04-06 19:19:17 +00008741 }
8742 }
8743 break;
8744
Chris Lattner503221f2006-01-13 21:28:09 +00008745 case Intrinsic::stackrestore: {
8746 // If the save is right next to the restore, remove the restore. This can
8747 // happen when variable allocas are DCE'd.
8748 if (IntrinsicInst *SS = dyn_cast<IntrinsicInst>(II->getOperand(1))) {
8749 if (SS->getIntrinsicID() == Intrinsic::stacksave) {
8750 BasicBlock::iterator BI = SS;
8751 if (&*++BI == II)
8752 return EraseInstFromFunction(CI);
8753 }
8754 }
8755
Chris Lattner024f8c82008-02-18 06:12:38 +00008756 // Scan down this block to see if there is another stack restore in the
8757 // same block without an intervening call/alloca.
8758 BasicBlock::iterator BI = II;
Chris Lattner503221f2006-01-13 21:28:09 +00008759 TerminatorInst *TI = II->getParent()->getTerminator();
Chris Lattner024f8c82008-02-18 06:12:38 +00008760 bool CannotRemove = false;
8761 for (++BI; &*BI != TI; ++BI) {
8762 if (isa<AllocaInst>(BI)) {
8763 CannotRemove = true;
8764 break;
8765 }
8766 if (isa<CallInst>(BI)) {
8767 if (!isa<IntrinsicInst>(BI)) {
Chris Lattner503221f2006-01-13 21:28:09 +00008768 CannotRemove = true;
8769 break;
8770 }
Chris Lattner024f8c82008-02-18 06:12:38 +00008771 // If there is a stackrestore below this one, remove this one.
Chris Lattner503221f2006-01-13 21:28:09 +00008772 return EraseInstFromFunction(CI);
Chris Lattner024f8c82008-02-18 06:12:38 +00008773 }
Chris Lattner503221f2006-01-13 21:28:09 +00008774 }
Chris Lattner024f8c82008-02-18 06:12:38 +00008775
8776 // If the stack restore is in a return/unwind block and if there are no
8777 // allocas or calls between the restore and the return, nuke the restore.
8778 if (!CannotRemove && (isa<ReturnInst>(TI) || isa<UnwindInst>(TI)))
8779 return EraseInstFromFunction(CI);
Chris Lattner503221f2006-01-13 21:28:09 +00008780 break;
8781 }
8782 }
Chris Lattner00648e12004-10-12 04:52:52 +00008783 }
8784
Chris Lattnerc66b2232006-01-13 20:11:04 +00008785 return visitCallSite(II);
Chris Lattner970c33a2003-06-19 17:00:31 +00008786}
8787
8788// InvokeInst simplification
8789//
8790Instruction *InstCombiner::visitInvokeInst(InvokeInst &II) {
Chris Lattneraec3d942003-10-07 22:32:43 +00008791 return visitCallSite(&II);
Chris Lattner970c33a2003-06-19 17:00:31 +00008792}
8793
Dale Johannesen0d1d3df2008-04-25 21:16:07 +00008794/// isSafeToEliminateVarargsCast - If this cast does not affect the value
8795/// passed through the varargs area, we can eliminate the use of the cast.
Dale Johannesenf6e15a42008-04-23 18:34:37 +00008796static bool isSafeToEliminateVarargsCast(const CallSite CS,
8797 const CastInst * const CI,
8798 const TargetData * const TD,
8799 const int ix) {
8800 if (!CI->isLosslessCast())
8801 return false;
8802
8803 // The size of ByVal arguments is derived from the type, so we
8804 // can't change to a type with a different size. If the size were
8805 // passed explicitly we could avoid this check.
8806 if (!CS.paramHasAttr(ix, ParamAttr::ByVal))
8807 return true;
8808
8809 const Type* SrcTy =
8810 cast<PointerType>(CI->getOperand(0)->getType())->getElementType();
8811 const Type* DstTy = cast<PointerType>(CI->getType())->getElementType();
8812 if (!SrcTy->isSized() || !DstTy->isSized())
8813 return false;
8814 if (TD->getABITypeSize(SrcTy) != TD->getABITypeSize(DstTy))
8815 return false;
8816 return true;
8817}
8818
Chris Lattneraec3d942003-10-07 22:32:43 +00008819// visitCallSite - Improvements for call and invoke instructions.
8820//
8821Instruction *InstCombiner::visitCallSite(CallSite CS) {
Chris Lattner75b4d1d2003-10-07 22:54:13 +00008822 bool Changed = false;
8823
8824 // If the callee is a constexpr cast of a function, attempt to move the cast
8825 // to the arguments of the call/invoke.
Chris Lattneraec3d942003-10-07 22:32:43 +00008826 if (transformConstExprCastCall(CS)) return 0;
8827
Chris Lattner75b4d1d2003-10-07 22:54:13 +00008828 Value *Callee = CS.getCalledValue();
Chris Lattner81a7a232004-10-16 18:11:37 +00008829
Chris Lattner61d9d812005-05-13 07:09:09 +00008830 if (Function *CalleeF = dyn_cast<Function>(Callee))
8831 if (CalleeF->getCallingConv() != CS.getCallingConv()) {
8832 Instruction *OldCall = CS.getInstruction();
8833 // If the call and callee calling conventions don't match, this call must
8834 // be unreachable, as the call is undefined.
Zhou Sheng75b871f2007-01-11 12:24:14 +00008835 new StoreInst(ConstantInt::getTrue(),
Christopher Lambedf07882007-12-17 01:12:55 +00008836 UndefValue::get(PointerType::getUnqual(Type::Int1Ty)),
8837 OldCall);
Chris Lattner61d9d812005-05-13 07:09:09 +00008838 if (!OldCall->use_empty())
8839 OldCall->replaceAllUsesWith(UndefValue::get(OldCall->getType()));
8840 if (isa<CallInst>(OldCall)) // Not worth removing an invoke here.
8841 return EraseInstFromFunction(*OldCall);
8842 return 0;
8843 }
8844
Chris Lattner8ba9ec92004-10-18 02:59:09 +00008845 if (isa<ConstantPointerNull>(Callee) || isa<UndefValue>(Callee)) {
8846 // This instruction is not reachable, just remove it. We insert a store to
8847 // undef so that we know that this code is not reachable, despite the fact
8848 // that we can't modify the CFG here.
Zhou Sheng75b871f2007-01-11 12:24:14 +00008849 new StoreInst(ConstantInt::getTrue(),
Christopher Lambedf07882007-12-17 01:12:55 +00008850 UndefValue::get(PointerType::getUnqual(Type::Int1Ty)),
Chris Lattner8ba9ec92004-10-18 02:59:09 +00008851 CS.getInstruction());
8852
8853 if (!CS.getInstruction()->use_empty())
8854 CS.getInstruction()->
8855 replaceAllUsesWith(UndefValue::get(CS.getInstruction()->getType()));
8856
8857 if (InvokeInst *II = dyn_cast<InvokeInst>(CS.getInstruction())) {
8858 // Don't break the CFG, insert a dummy cond branch.
Gabor Greife9ecc682008-04-06 20:25:17 +00008859 BranchInst::Create(II->getNormalDest(), II->getUnwindDest(),
8860 ConstantInt::getTrue(), II);
Chris Lattner81a7a232004-10-16 18:11:37 +00008861 }
Chris Lattner8ba9ec92004-10-18 02:59:09 +00008862 return EraseInstFromFunction(*CS.getInstruction());
8863 }
Chris Lattner81a7a232004-10-16 18:11:37 +00008864
Duncan Sands6d5da712007-09-17 10:26:40 +00008865 if (BitCastInst *BC = dyn_cast<BitCastInst>(Callee))
8866 if (IntrinsicInst *In = dyn_cast<IntrinsicInst>(BC->getOperand(0)))
8867 if (In->getIntrinsicID() == Intrinsic::init_trampoline)
8868 return transformCallThroughTrampoline(CS);
8869
Chris Lattner75b4d1d2003-10-07 22:54:13 +00008870 const PointerType *PTy = cast<PointerType>(Callee->getType());
8871 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
8872 if (FTy->isVarArg()) {
Dale Johannesen493527d2008-04-23 01:03:05 +00008873 int ix = FTy->getNumParams() + (isa<InvokeInst>(Callee) ? 3 : 1);
Chris Lattner75b4d1d2003-10-07 22:54:13 +00008874 // See if we can optimize any arguments passed through the varargs area of
8875 // the call.
8876 for (CallSite::arg_iterator I = CS.arg_begin()+FTy->getNumParams(),
Dale Johannesenf6e15a42008-04-23 18:34:37 +00008877 E = CS.arg_end(); I != E; ++I, ++ix) {
8878 CastInst *CI = dyn_cast<CastInst>(*I);
8879 if (CI && isSafeToEliminateVarargsCast(CS, CI, TD, ix)) {
8880 *I = CI->getOperand(0);
8881 Changed = true;
Chris Lattner75b4d1d2003-10-07 22:54:13 +00008882 }
Dale Johannesenf6e15a42008-04-23 18:34:37 +00008883 }
Chris Lattner75b4d1d2003-10-07 22:54:13 +00008884 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00008885
Duncan Sandsaa31b922007-12-19 21:13:37 +00008886 if (isa<InlineAsm>(Callee) && !CS.doesNotThrow()) {
Duncan Sands8e4847e2007-12-16 15:51:49 +00008887 // Inline asm calls cannot throw - mark them 'nounwind'.
Duncan Sandsaa31b922007-12-19 21:13:37 +00008888 CS.setDoesNotThrow();
Duncan Sands8e4847e2007-12-16 15:51:49 +00008889 Changed = true;
8890 }
8891
Chris Lattner75b4d1d2003-10-07 22:54:13 +00008892 return Changed ? CS.getInstruction() : 0;
Chris Lattneraec3d942003-10-07 22:32:43 +00008893}
8894
Chris Lattner970c33a2003-06-19 17:00:31 +00008895// transformConstExprCastCall - If the callee is a constexpr cast of a function,
8896// attempt to move the cast to the arguments of the call/invoke.
8897//
8898bool InstCombiner::transformConstExprCastCall(CallSite CS) {
8899 if (!isa<ConstantExpr>(CS.getCalledValue())) return false;
8900 ConstantExpr *CE = cast<ConstantExpr>(CS.getCalledValue());
Reid Spencer6c38f0b2006-11-27 01:05:10 +00008901 if (CE->getOpcode() != Instruction::BitCast ||
8902 !isa<Function>(CE->getOperand(0)))
Chris Lattner970c33a2003-06-19 17:00:31 +00008903 return false;
Reid Spencer87436872004-07-18 00:38:32 +00008904 Function *Callee = cast<Function>(CE->getOperand(0));
Chris Lattner970c33a2003-06-19 17:00:31 +00008905 Instruction *Caller = CS.getInstruction();
Chris Lattner8a923e72008-03-12 17:45:29 +00008906 const PAListPtr &CallerPAL = CS.getParamAttrs();
Chris Lattner970c33a2003-06-19 17:00:31 +00008907
8908 // Okay, this is a cast from a function to a different type. Unless doing so
8909 // would cause a type conversion of one of our arguments, change this call to
8910 // be a direct call with arguments casted to the appropriate types.
8911 //
8912 const FunctionType *FT = Callee->getFunctionType();
8913 const Type *OldRetTy = Caller->getType();
8914
Devang Patel70c238a2008-03-11 18:04:06 +00008915 if (isa<StructType>(FT->getReturnType()))
8916 return false; // TODO: Handle multiple return values.
8917
Chris Lattner1f7942f2004-01-14 06:06:08 +00008918 // Check to see if we are changing the return type...
8919 if (OldRetTy != FT->getReturnType()) {
Reid Spencer5301e7c2007-01-30 20:08:39 +00008920 if (Callee->isDeclaration() && !Caller->use_empty() &&
Chris Lattner7051d752007-01-06 19:53:32 +00008921 // Conversion is ok if changing from pointer to int of same size.
8922 !(isa<PointerType>(FT->getReturnType()) &&
8923 TD->getIntPtrType() == OldRetTy))
Chris Lattner400f9592007-01-06 02:09:32 +00008924 return false; // Cannot transform this return value.
Chris Lattner1f7942f2004-01-14 06:06:08 +00008925
Duncan Sands55e50902008-01-06 10:12:28 +00008926 if (!Caller->use_empty() &&
Duncan Sands55e50902008-01-06 10:12:28 +00008927 // void -> non-void is handled specially
Duncan Sands781f6542008-01-13 08:02:44 +00008928 FT->getReturnType() != Type::VoidTy &&
8929 !CastInst::isCastable(FT->getReturnType(), OldRetTy))
Duncan Sands55e50902008-01-06 10:12:28 +00008930 return false; // Cannot transform this return value.
8931
Chris Lattner8a923e72008-03-12 17:45:29 +00008932 if (!CallerPAL.isEmpty() && !Caller->use_empty()) {
8933 ParameterAttributes RAttrs = CallerPAL.getParamAttrs(0);
Duncan Sandsb18c30a2008-01-07 17:16:06 +00008934 if (RAttrs & ParamAttr::typeIncompatible(FT->getReturnType()))
8935 return false; // Attribute not compatible with transformed value.
8936 }
Duncan Sands404eb052008-01-06 18:27:01 +00008937
Chris Lattner1f7942f2004-01-14 06:06:08 +00008938 // If the callsite is an invoke instruction, and the return value is used by
8939 // a PHI node in a successor, we cannot change the return type of the call
8940 // because there is no place to put the cast instruction (without breaking
8941 // the critical edge). Bail out in this case.
8942 if (!Caller->use_empty())
8943 if (InvokeInst *II = dyn_cast<InvokeInst>(Caller))
8944 for (Value::use_iterator UI = II->use_begin(), E = II->use_end();
8945 UI != E; ++UI)
8946 if (PHINode *PN = dyn_cast<PHINode>(*UI))
8947 if (PN->getParent() == II->getNormalDest() ||
Chris Lattnerfae8ab32004-02-08 21:44:31 +00008948 PN->getParent() == II->getUnwindDest())
Chris Lattner1f7942f2004-01-14 06:06:08 +00008949 return false;
8950 }
Chris Lattner970c33a2003-06-19 17:00:31 +00008951
8952 unsigned NumActualArgs = unsigned(CS.arg_end()-CS.arg_begin());
8953 unsigned NumCommonArgs = std::min(FT->getNumParams(), NumActualArgs);
Misha Brukmanb1c93172005-04-21 23:48:37 +00008954
Chris Lattner970c33a2003-06-19 17:00:31 +00008955 CallSite::arg_iterator AI = CS.arg_begin();
8956 for (unsigned i = 0, e = NumCommonArgs; i != e; ++i, ++AI) {
8957 const Type *ParamTy = FT->getParamType(i);
Andrew Lenharthebfa24e2006-06-28 01:01:52 +00008958 const Type *ActTy = (*AI)->getType();
Duncan Sands55e50902008-01-06 10:12:28 +00008959
8960 if (!CastInst::isCastable(ActTy, ParamTy))
Duncan Sands404eb052008-01-06 18:27:01 +00008961 return false; // Cannot transform this parameter value.
8962
Chris Lattner8a923e72008-03-12 17:45:29 +00008963 if (CallerPAL.getParamAttrs(i + 1) & ParamAttr::typeIncompatible(ParamTy))
8964 return false; // Attribute not compatible with transformed value.
Duncan Sands55e50902008-01-06 10:12:28 +00008965
Reid Spencer6c38f0b2006-11-27 01:05:10 +00008966 ConstantInt *c = dyn_cast<ConstantInt>(*AI);
Duncan Sands55e50902008-01-06 10:12:28 +00008967 // Some conversions are safe even if we do not have a body.
8968 // Either we can cast directly, or we can upconvert the argument
Chris Lattner400f9592007-01-06 02:09:32 +00008969 bool isConvertible = ActTy == ParamTy ||
Chris Lattner7051d752007-01-06 19:53:32 +00008970 (isa<PointerType>(ParamTy) && isa<PointerType>(ActTy)) ||
Chris Lattner03c49532007-01-15 02:27:26 +00008971 (ParamTy->isInteger() && ActTy->isInteger() &&
Reid Spencer8f166b02007-01-08 16:32:00 +00008972 ParamTy->getPrimitiveSizeInBits() >= ActTy->getPrimitiveSizeInBits()) ||
8973 (c && ParamTy->getPrimitiveSizeInBits() >= ActTy->getPrimitiveSizeInBits()
Zhou Sheng222d5eb2007-03-25 05:01:29 +00008974 && c->getValue().isStrictlyPositive());
Reid Spencer5301e7c2007-01-30 20:08:39 +00008975 if (Callee->isDeclaration() && !isConvertible) return false;
Chris Lattner970c33a2003-06-19 17:00:31 +00008976 }
8977
8978 if (FT->getNumParams() < NumActualArgs && !FT->isVarArg() &&
Reid Spencer5301e7c2007-01-30 20:08:39 +00008979 Callee->isDeclaration())
Chris Lattner8a923e72008-03-12 17:45:29 +00008980 return false; // Do not delete arguments unless we have a function body.
Chris Lattner970c33a2003-06-19 17:00:31 +00008981
Chris Lattner8a923e72008-03-12 17:45:29 +00008982 if (FT->getNumParams() < NumActualArgs && FT->isVarArg() &&
8983 !CallerPAL.isEmpty())
Duncan Sands404eb052008-01-06 18:27:01 +00008984 // In this case we have more arguments than the new function type, but we
Duncan Sands781f6542008-01-13 08:02:44 +00008985 // won't be dropping them. Check that these extra arguments have attributes
8986 // that are compatible with being a vararg call argument.
Chris Lattner8a923e72008-03-12 17:45:29 +00008987 for (unsigned i = CallerPAL.getNumSlots(); i; --i) {
8988 if (CallerPAL.getSlot(i - 1).Index <= FT->getNumParams())
Duncan Sands781f6542008-01-13 08:02:44 +00008989 break;
Chris Lattner8a923e72008-03-12 17:45:29 +00008990 ParameterAttributes PAttrs = CallerPAL.getSlot(i - 1).Attrs;
Duncan Sands781f6542008-01-13 08:02:44 +00008991 if (PAttrs & ParamAttr::VarArgsIncompatible)
8992 return false;
8993 }
Duncan Sands404eb052008-01-06 18:27:01 +00008994
Chris Lattner970c33a2003-06-19 17:00:31 +00008995 // Okay, we decided that this is a safe thing to do: go ahead and start
8996 // inserting cast instructions as necessary...
8997 std::vector<Value*> Args;
8998 Args.reserve(NumActualArgs);
Chris Lattner8a923e72008-03-12 17:45:29 +00008999 SmallVector<ParamAttrsWithIndex, 8> attrVec;
Duncan Sands404eb052008-01-06 18:27:01 +00009000 attrVec.reserve(NumCommonArgs);
9001
9002 // Get any return attributes.
Chris Lattner8a923e72008-03-12 17:45:29 +00009003 ParameterAttributes RAttrs = CallerPAL.getParamAttrs(0);
Duncan Sands404eb052008-01-06 18:27:01 +00009004
9005 // If the return value is not being used, the type may not be compatible
9006 // with the existing attributes. Wipe out any problematic attributes.
Duncan Sandsb18c30a2008-01-07 17:16:06 +00009007 RAttrs &= ~ParamAttr::typeIncompatible(FT->getReturnType());
Duncan Sands404eb052008-01-06 18:27:01 +00009008
9009 // Add the new return attributes.
9010 if (RAttrs)
9011 attrVec.push_back(ParamAttrsWithIndex::get(0, RAttrs));
Chris Lattner970c33a2003-06-19 17:00:31 +00009012
9013 AI = CS.arg_begin();
9014 for (unsigned i = 0; i != NumCommonArgs; ++i, ++AI) {
9015 const Type *ParamTy = FT->getParamType(i);
9016 if ((*AI)->getType() == ParamTy) {
9017 Args.push_back(*AI);
9018 } else {
Reid Spencer668d90f2006-12-18 08:47:13 +00009019 Instruction::CastOps opcode = CastInst::getCastOpcode(*AI,
Reid Spencerc635f472006-12-31 05:48:39 +00009020 false, ParamTy, false);
Reid Spencer668d90f2006-12-18 08:47:13 +00009021 CastInst *NewCast = CastInst::create(opcode, *AI, ParamTy, "tmp");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00009022 Args.push_back(InsertNewInstBefore(NewCast, *Caller));
Chris Lattner970c33a2003-06-19 17:00:31 +00009023 }
Duncan Sands404eb052008-01-06 18:27:01 +00009024
9025 // Add any parameter attributes.
Chris Lattner8a923e72008-03-12 17:45:29 +00009026 if (ParameterAttributes PAttrs = CallerPAL.getParamAttrs(i + 1))
Duncan Sands404eb052008-01-06 18:27:01 +00009027 attrVec.push_back(ParamAttrsWithIndex::get(i + 1, PAttrs));
Chris Lattner970c33a2003-06-19 17:00:31 +00009028 }
9029
9030 // If the function takes more arguments than the call was taking, add them
9031 // now...
9032 for (unsigned i = NumCommonArgs; i != FT->getNumParams(); ++i)
9033 Args.push_back(Constant::getNullValue(FT->getParamType(i)));
9034
9035 // If we are removing arguments to the function, emit an obnoxious warning...
Anton Korobeynikov1bfd1212008-02-20 11:26:25 +00009036 if (FT->getNumParams() < NumActualArgs) {
Chris Lattner970c33a2003-06-19 17:00:31 +00009037 if (!FT->isVarArg()) {
Bill Wendlingf3baad32006-12-07 01:30:32 +00009038 cerr << "WARNING: While resolving call to function '"
9039 << Callee->getName() << "' arguments were dropped!\n";
Chris Lattner970c33a2003-06-19 17:00:31 +00009040 } else {
9041 // Add all of the arguments in their promoted form to the arg list...
9042 for (unsigned i = FT->getNumParams(); i != NumActualArgs; ++i, ++AI) {
9043 const Type *PTy = getPromotedType((*AI)->getType());
9044 if (PTy != (*AI)->getType()) {
9045 // Must promote to pass through va_arg area!
Reid Spencerc635f472006-12-31 05:48:39 +00009046 Instruction::CastOps opcode = CastInst::getCastOpcode(*AI, false,
9047 PTy, false);
Reid Spencer668d90f2006-12-18 08:47:13 +00009048 Instruction *Cast = CastInst::create(opcode, *AI, PTy, "tmp");
Chris Lattner970c33a2003-06-19 17:00:31 +00009049 InsertNewInstBefore(Cast, *Caller);
9050 Args.push_back(Cast);
9051 } else {
9052 Args.push_back(*AI);
9053 }
Duncan Sands404eb052008-01-06 18:27:01 +00009054
Duncan Sands781f6542008-01-13 08:02:44 +00009055 // Add any parameter attributes.
Chris Lattner8a923e72008-03-12 17:45:29 +00009056 if (ParameterAttributes PAttrs = CallerPAL.getParamAttrs(i + 1))
Duncan Sands781f6542008-01-13 08:02:44 +00009057 attrVec.push_back(ParamAttrsWithIndex::get(i + 1, PAttrs));
9058 }
Chris Lattner970c33a2003-06-19 17:00:31 +00009059 }
Anton Korobeynikov1bfd1212008-02-20 11:26:25 +00009060 }
Chris Lattner970c33a2003-06-19 17:00:31 +00009061
9062 if (FT->getReturnType() == Type::VoidTy)
Chris Lattner6e0123b2007-02-11 01:23:03 +00009063 Caller->setName(""); // Void type should not have a name.
Chris Lattner970c33a2003-06-19 17:00:31 +00009064
Chris Lattner8a923e72008-03-12 17:45:29 +00009065 const PAListPtr &NewCallerPAL = PAListPtr::get(attrVec.begin(),attrVec.end());
Duncan Sands404eb052008-01-06 18:27:01 +00009066
Chris Lattner970c33a2003-06-19 17:00:31 +00009067 Instruction *NC;
9068 if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) {
Gabor Greife9ecc682008-04-06 20:25:17 +00009069 NC = InvokeInst::Create(Callee, II->getNormalDest(), II->getUnwindDest(),
9070 Args.begin(), Args.end(), Caller->getName(), Caller);
Reid Spencerdff9d692007-07-30 19:53:57 +00009071 cast<InvokeInst>(NC)->setCallingConv(II->getCallingConv());
Duncan Sands404eb052008-01-06 18:27:01 +00009072 cast<InvokeInst>(NC)->setParamAttrs(NewCallerPAL);
Chris Lattner970c33a2003-06-19 17:00:31 +00009073 } else {
Gabor Greife9ecc682008-04-06 20:25:17 +00009074 NC = CallInst::Create(Callee, Args.begin(), Args.end(),
9075 Caller->getName(), Caller);
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00009076 CallInst *CI = cast<CallInst>(Caller);
9077 if (CI->isTailCall())
Chris Lattner6aacb0f2005-05-06 06:48:21 +00009078 cast<CallInst>(NC)->setTailCall();
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00009079 cast<CallInst>(NC)->setCallingConv(CI->getCallingConv());
Duncan Sands404eb052008-01-06 18:27:01 +00009080 cast<CallInst>(NC)->setParamAttrs(NewCallerPAL);
Chris Lattner970c33a2003-06-19 17:00:31 +00009081 }
9082
Chris Lattner6e0123b2007-02-11 01:23:03 +00009083 // Insert a cast of the return type as necessary.
Chris Lattner970c33a2003-06-19 17:00:31 +00009084 Value *NV = NC;
Duncan Sands55e50902008-01-06 10:12:28 +00009085 if (OldRetTy != NV->getType() && !Caller->use_empty()) {
Chris Lattner970c33a2003-06-19 17:00:31 +00009086 if (NV->getType() != Type::VoidTy) {
Reid Spencerc635f472006-12-31 05:48:39 +00009087 Instruction::CastOps opcode = CastInst::getCastOpcode(NC, false,
Duncan Sands55e50902008-01-06 10:12:28 +00009088 OldRetTy, false);
9089 NV = NC = CastInst::create(opcode, NC, OldRetTy, "tmp");
Chris Lattner686767f2003-10-30 00:46:41 +00009090
9091 // If this is an invoke instruction, we should insert it after the first
9092 // non-phi, instruction in the normal successor block.
9093 if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) {
9094 BasicBlock::iterator I = II->getNormalDest()->begin();
9095 while (isa<PHINode>(I)) ++I;
9096 InsertNewInstBefore(NC, *I);
9097 } else {
9098 // Otherwise, it's a call, just insert cast right after the call instr
9099 InsertNewInstBefore(NC, *Caller);
9100 }
Chris Lattner51ea1272004-02-28 05:22:00 +00009101 AddUsersToWorkList(*Caller);
Chris Lattner970c33a2003-06-19 17:00:31 +00009102 } else {
Chris Lattnere29d6342004-10-17 21:22:38 +00009103 NV = UndefValue::get(Caller->getType());
Chris Lattner970c33a2003-06-19 17:00:31 +00009104 }
9105 }
9106
9107 if (Caller->getType() != Type::VoidTy && !Caller->use_empty())
9108 Caller->replaceAllUsesWith(NV);
Chris Lattner51f54572007-03-02 19:59:19 +00009109 Caller->eraseFromParent();
Chris Lattnerb15e2b12007-03-02 21:28:56 +00009110 RemoveFromWorkList(Caller);
Chris Lattner970c33a2003-06-19 17:00:31 +00009111 return true;
9112}
9113
Duncan Sands6d5da712007-09-17 10:26:40 +00009114// transformCallThroughTrampoline - Turn a call to a function created by the
9115// init_trampoline intrinsic into a direct call to the underlying function.
9116//
9117Instruction *InstCombiner::transformCallThroughTrampoline(CallSite CS) {
9118 Value *Callee = CS.getCalledValue();
9119 const PointerType *PTy = cast<PointerType>(Callee->getType());
9120 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
Chris Lattner8a923e72008-03-12 17:45:29 +00009121 const PAListPtr &Attrs = CS.getParamAttrs();
Duncan Sandsb5ca2e92008-01-14 19:52:09 +00009122
9123 // If the call already has the 'nest' attribute somewhere then give up -
9124 // otherwise 'nest' would occur twice after splicing in the chain.
Chris Lattner8a923e72008-03-12 17:45:29 +00009125 if (Attrs.hasAttrSomewhere(ParamAttr::Nest))
Duncan Sandsb5ca2e92008-01-14 19:52:09 +00009126 return 0;
Duncan Sands6d5da712007-09-17 10:26:40 +00009127
9128 IntrinsicInst *Tramp =
9129 cast<IntrinsicInst>(cast<BitCastInst>(Callee)->getOperand(0));
9130
9131 Function *NestF =
9132 cast<Function>(IntrinsicInst::StripPointerCasts(Tramp->getOperand(2)));
9133 const PointerType *NestFPTy = cast<PointerType>(NestF->getType());
9134 const FunctionType *NestFTy = cast<FunctionType>(NestFPTy->getElementType());
9135
Chris Lattner8a923e72008-03-12 17:45:29 +00009136 const PAListPtr &NestAttrs = NestF->getParamAttrs();
9137 if (!NestAttrs.isEmpty()) {
Duncan Sands6d5da712007-09-17 10:26:40 +00009138 unsigned NestIdx = 1;
9139 const Type *NestTy = 0;
Dale Johannesen89268bc2008-02-19 21:38:47 +00009140 ParameterAttributes NestAttr = ParamAttr::None;
Duncan Sands6d5da712007-09-17 10:26:40 +00009141
9142 // Look for a parameter marked with the 'nest' attribute.
9143 for (FunctionType::param_iterator I = NestFTy->param_begin(),
9144 E = NestFTy->param_end(); I != E; ++NestIdx, ++I)
Chris Lattner8a923e72008-03-12 17:45:29 +00009145 if (NestAttrs.paramHasAttr(NestIdx, ParamAttr::Nest)) {
Duncan Sands6d5da712007-09-17 10:26:40 +00009146 // Record the parameter type and any other attributes.
9147 NestTy = *I;
Chris Lattner8a923e72008-03-12 17:45:29 +00009148 NestAttr = NestAttrs.getParamAttrs(NestIdx);
Duncan Sands6d5da712007-09-17 10:26:40 +00009149 break;
9150 }
9151
9152 if (NestTy) {
9153 Instruction *Caller = CS.getInstruction();
9154 std::vector<Value*> NewArgs;
9155 NewArgs.reserve(unsigned(CS.arg_end()-CS.arg_begin())+1);
9156
Chris Lattner8a923e72008-03-12 17:45:29 +00009157 SmallVector<ParamAttrsWithIndex, 8> NewAttrs;
9158 NewAttrs.reserve(Attrs.getNumSlots() + 1);
Duncan Sandsb5ca2e92008-01-14 19:52:09 +00009159
Duncan Sands6d5da712007-09-17 10:26:40 +00009160 // Insert the nest argument into the call argument list, which may
Duncan Sandsb5ca2e92008-01-14 19:52:09 +00009161 // mean appending it. Likewise for attributes.
9162
9163 // Add any function result attributes.
Chris Lattner8a923e72008-03-12 17:45:29 +00009164 if (ParameterAttributes Attr = Attrs.getParamAttrs(0))
9165 NewAttrs.push_back(ParamAttrsWithIndex::get(0, Attr));
Duncan Sandsb5ca2e92008-01-14 19:52:09 +00009166
Duncan Sands6d5da712007-09-17 10:26:40 +00009167 {
9168 unsigned Idx = 1;
9169 CallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end();
9170 do {
9171 if (Idx == NestIdx) {
Duncan Sandsb5ca2e92008-01-14 19:52:09 +00009172 // Add the chain argument and attributes.
Duncan Sands6d5da712007-09-17 10:26:40 +00009173 Value *NestVal = Tramp->getOperand(3);
9174 if (NestVal->getType() != NestTy)
9175 NestVal = new BitCastInst(NestVal, NestTy, "nest", Caller);
9176 NewArgs.push_back(NestVal);
Duncan Sandsb5ca2e92008-01-14 19:52:09 +00009177 NewAttrs.push_back(ParamAttrsWithIndex::get(NestIdx, NestAttr));
Duncan Sands6d5da712007-09-17 10:26:40 +00009178 }
9179
9180 if (I == E)
9181 break;
9182
Duncan Sandsb5ca2e92008-01-14 19:52:09 +00009183 // Add the original argument and attributes.
Duncan Sands6d5da712007-09-17 10:26:40 +00009184 NewArgs.push_back(*I);
Chris Lattner8a923e72008-03-12 17:45:29 +00009185 if (ParameterAttributes Attr = Attrs.getParamAttrs(Idx))
Duncan Sandsb5ca2e92008-01-14 19:52:09 +00009186 NewAttrs.push_back
9187 (ParamAttrsWithIndex::get(Idx + (Idx >= NestIdx), Attr));
Duncan Sands6d5da712007-09-17 10:26:40 +00009188
9189 ++Idx, ++I;
9190 } while (1);
9191 }
9192
9193 // The trampoline may have been bitcast to a bogus type (FTy).
9194 // Handle this by synthesizing a new function type, equal to FTy
Duncan Sandsb5ca2e92008-01-14 19:52:09 +00009195 // with the chain parameter inserted.
Duncan Sands6d5da712007-09-17 10:26:40 +00009196
Duncan Sands6d5da712007-09-17 10:26:40 +00009197 std::vector<const Type*> NewTypes;
Duncan Sands6d5da712007-09-17 10:26:40 +00009198 NewTypes.reserve(FTy->getNumParams()+1);
9199
Duncan Sands6d5da712007-09-17 10:26:40 +00009200 // Insert the chain's type into the list of parameter types, which may
Duncan Sandsb5ca2e92008-01-14 19:52:09 +00009201 // mean appending it.
Duncan Sands6d5da712007-09-17 10:26:40 +00009202 {
9203 unsigned Idx = 1;
9204 FunctionType::param_iterator I = FTy->param_begin(),
9205 E = FTy->param_end();
9206
9207 do {
Duncan Sandsb5ca2e92008-01-14 19:52:09 +00009208 if (Idx == NestIdx)
9209 // Add the chain's type.
Duncan Sands6d5da712007-09-17 10:26:40 +00009210 NewTypes.push_back(NestTy);
Duncan Sands6d5da712007-09-17 10:26:40 +00009211
9212 if (I == E)
9213 break;
9214
Duncan Sandsb5ca2e92008-01-14 19:52:09 +00009215 // Add the original type.
Duncan Sands6d5da712007-09-17 10:26:40 +00009216 NewTypes.push_back(*I);
Duncan Sands6d5da712007-09-17 10:26:40 +00009217
9218 ++Idx, ++I;
9219 } while (1);
9220 }
9221
9222 // Replace the trampoline call with a direct call. Let the generic
9223 // code sort out any function type mismatches.
9224 FunctionType *NewFTy =
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00009225 FunctionType::get(FTy->getReturnType(), NewTypes, FTy->isVarArg());
Christopher Lambedf07882007-12-17 01:12:55 +00009226 Constant *NewCallee = NestF->getType() == PointerType::getUnqual(NewFTy) ?
9227 NestF : ConstantExpr::getBitCast(NestF, PointerType::getUnqual(NewFTy));
Chris Lattner8a923e72008-03-12 17:45:29 +00009228 const PAListPtr &NewPAL = PAListPtr::get(NewAttrs.begin(),NewAttrs.end());
Duncan Sands6d5da712007-09-17 10:26:40 +00009229
9230 Instruction *NewCaller;
9231 if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) {
Gabor Greife9ecc682008-04-06 20:25:17 +00009232 NewCaller = InvokeInst::Create(NewCallee,
9233 II->getNormalDest(), II->getUnwindDest(),
9234 NewArgs.begin(), NewArgs.end(),
9235 Caller->getName(), Caller);
Duncan Sands6d5da712007-09-17 10:26:40 +00009236 cast<InvokeInst>(NewCaller)->setCallingConv(II->getCallingConv());
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00009237 cast<InvokeInst>(NewCaller)->setParamAttrs(NewPAL);
Duncan Sands6d5da712007-09-17 10:26:40 +00009238 } else {
Gabor Greife9ecc682008-04-06 20:25:17 +00009239 NewCaller = CallInst::Create(NewCallee, NewArgs.begin(), NewArgs.end(),
9240 Caller->getName(), Caller);
Duncan Sands6d5da712007-09-17 10:26:40 +00009241 if (cast<CallInst>(Caller)->isTailCall())
9242 cast<CallInst>(NewCaller)->setTailCall();
9243 cast<CallInst>(NewCaller)->
9244 setCallingConv(cast<CallInst>(Caller)->getCallingConv());
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00009245 cast<CallInst>(NewCaller)->setParamAttrs(NewPAL);
Duncan Sands6d5da712007-09-17 10:26:40 +00009246 }
9247 if (Caller->getType() != Type::VoidTy && !Caller->use_empty())
9248 Caller->replaceAllUsesWith(NewCaller);
9249 Caller->eraseFromParent();
9250 RemoveFromWorkList(Caller);
9251 return 0;
9252 }
9253 }
9254
9255 // Replace the trampoline call with a direct call. Since there is no 'nest'
9256 // parameter, there is no need to adjust the argument list. Let the generic
9257 // code sort out any function type mismatches.
9258 Constant *NewCallee =
9259 NestF->getType() == PTy ? NestF : ConstantExpr::getBitCast(NestF, PTy);
9260 CS.setCalledFunction(NewCallee);
9261 return CS.getInstruction();
9262}
9263
Chris Lattnercadac0c2006-11-01 04:51:18 +00009264/// FoldPHIArgBinOpIntoPHI - If we have something like phi [add (a,b), add(c,d)]
9265/// and if a/b/c/d and the add's all have a single use, turn this into two phi's
9266/// and a single binop.
9267Instruction *InstCombiner::FoldPHIArgBinOpIntoPHI(PHINode &PN) {
9268 Instruction *FirstInst = cast<Instruction>(PN.getIncomingValue(0));
Reid Spencer2341c222007-02-02 02:16:23 +00009269 assert(isa<BinaryOperator>(FirstInst) || isa<GetElementPtrInst>(FirstInst) ||
9270 isa<CmpInst>(FirstInst));
Chris Lattnercadac0c2006-11-01 04:51:18 +00009271 unsigned Opc = FirstInst->getOpcode();
Chris Lattnercd62f112006-11-08 19:29:23 +00009272 Value *LHSVal = FirstInst->getOperand(0);
9273 Value *RHSVal = FirstInst->getOperand(1);
9274
9275 const Type *LHSType = LHSVal->getType();
9276 const Type *RHSType = RHSVal->getType();
Chris Lattnercadac0c2006-11-01 04:51:18 +00009277
9278 // Scan to see if all operands are the same opcode, all have one use, and all
9279 // kill their operands (i.e. the operands have one use).
Chris Lattnerdc826fc2006-11-01 04:55:47 +00009280 for (unsigned i = 0; i != PN.getNumIncomingValues(); ++i) {
Chris Lattnercadac0c2006-11-01 04:51:18 +00009281 Instruction *I = dyn_cast<Instruction>(PN.getIncomingValue(i));
Chris Lattnerdc826fc2006-11-01 04:55:47 +00009282 if (!I || I->getOpcode() != Opc || !I->hasOneUse() ||
Reid Spencer266e42b2006-12-23 06:05:41 +00009283 // Verify type of the LHS matches so we don't fold cmp's of different
Chris Lattnereebea432006-11-01 07:43:41 +00009284 // types or GEP's with different index types.
9285 I->getOperand(0)->getType() != LHSType ||
9286 I->getOperand(1)->getType() != RHSType)
Chris Lattnercadac0c2006-11-01 04:51:18 +00009287 return 0;
Reid Spencer266e42b2006-12-23 06:05:41 +00009288
9289 // If they are CmpInst instructions, check their predicates
9290 if (Opc == Instruction::ICmp || Opc == Instruction::FCmp)
9291 if (cast<CmpInst>(I)->getPredicate() !=
9292 cast<CmpInst>(FirstInst)->getPredicate())
9293 return 0;
Chris Lattnercd62f112006-11-08 19:29:23 +00009294
9295 // Keep track of which operand needs a phi node.
9296 if (I->getOperand(0) != LHSVal) LHSVal = 0;
9297 if (I->getOperand(1) != RHSVal) RHSVal = 0;
Chris Lattnercadac0c2006-11-01 04:51:18 +00009298 }
9299
Chris Lattner4f218d52006-11-08 19:42:28 +00009300 // Otherwise, this is safe to transform, determine if it is profitable.
9301
9302 // If this is a GEP, and if the index (not the pointer) needs a PHI, bail out.
9303 // Indexes are often folded into load/store instructions, so we don't want to
9304 // hide them behind a phi.
9305 if (isa<GetElementPtrInst>(FirstInst) && RHSVal == 0)
9306 return 0;
9307
Chris Lattnercadac0c2006-11-01 04:51:18 +00009308 Value *InLHS = FirstInst->getOperand(0);
Chris Lattnercadac0c2006-11-01 04:51:18 +00009309 Value *InRHS = FirstInst->getOperand(1);
Chris Lattner4f218d52006-11-08 19:42:28 +00009310 PHINode *NewLHS = 0, *NewRHS = 0;
Chris Lattnercd62f112006-11-08 19:29:23 +00009311 if (LHSVal == 0) {
Gabor Greife9ecc682008-04-06 20:25:17 +00009312 NewLHS = PHINode::Create(LHSType, FirstInst->getOperand(0)->getName()+".pn");
Chris Lattnercd62f112006-11-08 19:29:23 +00009313 NewLHS->reserveOperandSpace(PN.getNumOperands()/2);
9314 NewLHS->addIncoming(InLHS, PN.getIncomingBlock(0));
Chris Lattnereebea432006-11-01 07:43:41 +00009315 InsertNewInstBefore(NewLHS, PN);
9316 LHSVal = NewLHS;
9317 }
Chris Lattnercd62f112006-11-08 19:29:23 +00009318
9319 if (RHSVal == 0) {
Gabor Greife9ecc682008-04-06 20:25:17 +00009320 NewRHS = PHINode::Create(RHSType, FirstInst->getOperand(1)->getName()+".pn");
Chris Lattnercd62f112006-11-08 19:29:23 +00009321 NewRHS->reserveOperandSpace(PN.getNumOperands()/2);
9322 NewRHS->addIncoming(InRHS, PN.getIncomingBlock(0));
Chris Lattnereebea432006-11-01 07:43:41 +00009323 InsertNewInstBefore(NewRHS, PN);
9324 RHSVal = NewRHS;
9325 }
9326
Chris Lattnercd62f112006-11-08 19:29:23 +00009327 // Add all operands to the new PHIs.
9328 for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) {
9329 if (NewLHS) {
9330 Value *NewInLHS =cast<Instruction>(PN.getIncomingValue(i))->getOperand(0);
9331 NewLHS->addIncoming(NewInLHS, PN.getIncomingBlock(i));
9332 }
9333 if (NewRHS) {
9334 Value *NewInRHS =cast<Instruction>(PN.getIncomingValue(i))->getOperand(1);
9335 NewRHS->addIncoming(NewInRHS, PN.getIncomingBlock(i));
9336 }
9337 }
9338
Chris Lattnercadac0c2006-11-01 04:51:18 +00009339 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(FirstInst))
Chris Lattnereebea432006-11-01 07:43:41 +00009340 return BinaryOperator::create(BinOp->getOpcode(), LHSVal, RHSVal);
Reid Spencer266e42b2006-12-23 06:05:41 +00009341 else if (CmpInst *CIOp = dyn_cast<CmpInst>(FirstInst))
9342 return CmpInst::create(CIOp->getOpcode(), CIOp->getPredicate(), LHSVal,
9343 RHSVal);
Chris Lattnereebea432006-11-01 07:43:41 +00009344 else {
9345 assert(isa<GetElementPtrInst>(FirstInst));
Gabor Greife9ecc682008-04-06 20:25:17 +00009346 return GetElementPtrInst::Create(LHSVal, RHSVal);
Chris Lattnereebea432006-11-01 07:43:41 +00009347 }
Chris Lattnercadac0c2006-11-01 04:51:18 +00009348}
9349
Chris Lattner14f82c72006-11-01 07:13:54 +00009350/// isSafeToSinkLoad - Return true if we know that it is safe sink the load out
9351/// of the block that defines it. This means that it must be obvious the value
9352/// of the load is not changed from the point of the load to the end of the
9353/// block it is in.
Chris Lattnerc9042052007-02-01 22:30:07 +00009354///
9355/// Finally, it is safe, but not profitable, to sink a load targetting a
9356/// non-address-taken alloca. Doing so will cause us to not promote the alloca
9357/// to a register.
Chris Lattner14f82c72006-11-01 07:13:54 +00009358static bool isSafeToSinkLoad(LoadInst *L) {
9359 BasicBlock::iterator BBI = L, E = L->getParent()->end();
9360
9361 for (++BBI; BBI != E; ++BBI)
9362 if (BBI->mayWriteToMemory())
9363 return false;
Chris Lattnerc9042052007-02-01 22:30:07 +00009364
9365 // Check for non-address taken alloca. If not address-taken already, it isn't
9366 // profitable to do this xform.
9367 if (AllocaInst *AI = dyn_cast<AllocaInst>(L->getOperand(0))) {
9368 bool isAddressTaken = false;
9369 for (Value::use_iterator UI = AI->use_begin(), E = AI->use_end();
9370 UI != E; ++UI) {
9371 if (isa<LoadInst>(UI)) continue;
9372 if (StoreInst *SI = dyn_cast<StoreInst>(*UI)) {
9373 // If storing TO the alloca, then the address isn't taken.
9374 if (SI->getOperand(1) == AI) continue;
9375 }
9376 isAddressTaken = true;
9377 break;
9378 }
9379
9380 if (!isAddressTaken)
9381 return false;
9382 }
9383
Chris Lattner14f82c72006-11-01 07:13:54 +00009384 return true;
9385}
9386
Chris Lattner970c33a2003-06-19 17:00:31 +00009387
Chris Lattner7515cab2004-11-14 19:13:23 +00009388// FoldPHIArgOpIntoPHI - If all operands to a PHI node are the same "unary"
9389// operator and they all are only used by the PHI, PHI together their
9390// inputs, and do the operation once, to the result of the PHI.
9391Instruction *InstCombiner::FoldPHIArgOpIntoPHI(PHINode &PN) {
9392 Instruction *FirstInst = cast<Instruction>(PN.getIncomingValue(0));
9393
9394 // Scan the instruction, looking for input operations that can be folded away.
9395 // If all input operands to the phi are the same instruction (e.g. a cast from
9396 // the same type or "+42") we can pull the operation through the PHI, reducing
9397 // code size and simplifying code.
9398 Constant *ConstantOp = 0;
9399 const Type *CastSrcTy = 0;
Chris Lattner14f82c72006-11-01 07:13:54 +00009400 bool isVolatile = false;
Chris Lattner7515cab2004-11-14 19:13:23 +00009401 if (isa<CastInst>(FirstInst)) {
9402 CastSrcTy = FirstInst->getOperand(0)->getType();
Reid Spencer2341c222007-02-02 02:16:23 +00009403 } else if (isa<BinaryOperator>(FirstInst) || isa<CmpInst>(FirstInst)) {
Reid Spencer266e42b2006-12-23 06:05:41 +00009404 // Can fold binop, compare or shift here if the RHS is a constant,
9405 // otherwise call FoldPHIArgBinOpIntoPHI.
Chris Lattner7515cab2004-11-14 19:13:23 +00009406 ConstantOp = dyn_cast<Constant>(FirstInst->getOperand(1));
Chris Lattnercadac0c2006-11-01 04:51:18 +00009407 if (ConstantOp == 0)
9408 return FoldPHIArgBinOpIntoPHI(PN);
Chris Lattner14f82c72006-11-01 07:13:54 +00009409 } else if (LoadInst *LI = dyn_cast<LoadInst>(FirstInst)) {
9410 isVolatile = LI->isVolatile();
9411 // We can't sink the load if the loaded value could be modified between the
9412 // load and the PHI.
9413 if (LI->getParent() != PN.getIncomingBlock(0) ||
9414 !isSafeToSinkLoad(LI))
9415 return 0;
Chris Lattnereebea432006-11-01 07:43:41 +00009416 } else if (isa<GetElementPtrInst>(FirstInst)) {
Chris Lattner4f218d52006-11-08 19:42:28 +00009417 if (FirstInst->getNumOperands() == 2)
Chris Lattnereebea432006-11-01 07:43:41 +00009418 return FoldPHIArgBinOpIntoPHI(PN);
9419 // Can't handle general GEPs yet.
9420 return 0;
Chris Lattner7515cab2004-11-14 19:13:23 +00009421 } else {
9422 return 0; // Cannot fold this operation.
9423 }
9424
9425 // Check to see if all arguments are the same operation.
9426 for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) {
9427 if (!isa<Instruction>(PN.getIncomingValue(i))) return 0;
9428 Instruction *I = cast<Instruction>(PN.getIncomingValue(i));
Reid Spencer266e42b2006-12-23 06:05:41 +00009429 if (!I->hasOneUse() || !I->isSameOperationAs(FirstInst))
Chris Lattner7515cab2004-11-14 19:13:23 +00009430 return 0;
9431 if (CastSrcTy) {
9432 if (I->getOperand(0)->getType() != CastSrcTy)
9433 return 0; // Cast operation must match.
Chris Lattner14f82c72006-11-01 07:13:54 +00009434 } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
Reid Spencer266e42b2006-12-23 06:05:41 +00009435 // We can't sink the load if the loaded value could be modified between
9436 // the load and the PHI.
Chris Lattner14f82c72006-11-01 07:13:54 +00009437 if (LI->isVolatile() != isVolatile ||
9438 LI->getParent() != PN.getIncomingBlock(i) ||
9439 !isSafeToSinkLoad(LI))
9440 return 0;
Chris Lattnerd9e3b5c2008-04-29 17:28:22 +00009441
9442 // If the PHI is volatile and its block has multiple successors, sinking
9443 // it would remove a load of the volatile value from the path through the
9444 // other successor.
9445 if (isVolatile &&
9446 LI->getParent()->getTerminator()->getNumSuccessors() != 1)
9447 return 0;
9448
9449
Chris Lattner7515cab2004-11-14 19:13:23 +00009450 } else if (I->getOperand(1) != ConstantOp) {
9451 return 0;
9452 }
9453 }
9454
9455 // Okay, they are all the same operation. Create a new PHI node of the
9456 // correct type, and PHI together all of the LHS's of the instructions.
Gabor Greife9ecc682008-04-06 20:25:17 +00009457 PHINode *NewPN = PHINode::Create(FirstInst->getOperand(0)->getType(),
9458 PN.getName()+".in");
Chris Lattnerd8e20182005-01-29 00:39:08 +00009459 NewPN->reserveOperandSpace(PN.getNumOperands()/2);
Chris Lattner46dd5a62004-11-14 19:29:34 +00009460
9461 Value *InVal = FirstInst->getOperand(0);
9462 NewPN->addIncoming(InVal, PN.getIncomingBlock(0));
Chris Lattner7515cab2004-11-14 19:13:23 +00009463
9464 // Add all operands to the new PHI.
Chris Lattner46dd5a62004-11-14 19:29:34 +00009465 for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) {
9466 Value *NewInVal = cast<Instruction>(PN.getIncomingValue(i))->getOperand(0);
9467 if (NewInVal != InVal)
9468 InVal = 0;
9469 NewPN->addIncoming(NewInVal, PN.getIncomingBlock(i));
9470 }
9471
9472 Value *PhiVal;
9473 if (InVal) {
9474 // The new PHI unions all of the same values together. This is really
9475 // common, so we handle it intelligently here for compile-time speed.
9476 PhiVal = InVal;
9477 delete NewPN;
9478 } else {
9479 InsertNewInstBefore(NewPN, PN);
9480 PhiVal = NewPN;
9481 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00009482
Chris Lattner7515cab2004-11-14 19:13:23 +00009483 // Insert and return the new operation.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00009484 if (CastInst* FirstCI = dyn_cast<CastInst>(FirstInst))
9485 return CastInst::create(FirstCI->getOpcode(), PhiVal, PN.getType());
Chris Lattner9233c122008-04-29 17:13:43 +00009486 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(FirstInst))
Chris Lattner46dd5a62004-11-14 19:29:34 +00009487 return BinaryOperator::create(BinOp->getOpcode(), PhiVal, ConstantOp);
Chris Lattner9233c122008-04-29 17:13:43 +00009488 if (CmpInst *CIOp = dyn_cast<CmpInst>(FirstInst))
Reid Spencer266e42b2006-12-23 06:05:41 +00009489 return CmpInst::create(CIOp->getOpcode(), CIOp->getPredicate(),
9490 PhiVal, ConstantOp);
Chris Lattner9233c122008-04-29 17:13:43 +00009491 assert(isa<LoadInst>(FirstInst) && "Unknown operation");
9492
9493 // If this was a volatile load that we are merging, make sure to loop through
9494 // and mark all the input loads as non-volatile. If we don't do this, we will
9495 // insert a new volatile load and the old ones will not be deletable.
9496 if (isVolatile)
9497 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i)
9498 cast<LoadInst>(PN.getIncomingValue(i))->setVolatile(false);
9499
9500 return new LoadInst(PhiVal, "", isVolatile);
Chris Lattner7515cab2004-11-14 19:13:23 +00009501}
Chris Lattner48a44f72002-05-02 17:06:02 +00009502
Chris Lattner71536432005-01-17 05:10:15 +00009503/// DeadPHICycle - Return true if this PHI node is only used by a PHI node cycle
9504/// that is dead.
Chris Lattnerd2602d52007-03-26 20:40:50 +00009505static bool DeadPHICycle(PHINode *PN,
9506 SmallPtrSet<PHINode*, 16> &PotentiallyDeadPHIs) {
Chris Lattner71536432005-01-17 05:10:15 +00009507 if (PN->use_empty()) return true;
9508 if (!PN->hasOneUse()) return false;
9509
9510 // Remember this node, and if we find the cycle, return.
Chris Lattnerd2602d52007-03-26 20:40:50 +00009511 if (!PotentiallyDeadPHIs.insert(PN))
Chris Lattner71536432005-01-17 05:10:15 +00009512 return true;
Chris Lattner0e258b82007-08-28 04:23:55 +00009513
9514 // Don't scan crazily complex things.
9515 if (PotentiallyDeadPHIs.size() == 16)
9516 return false;
Chris Lattner71536432005-01-17 05:10:15 +00009517
9518 if (PHINode *PU = dyn_cast<PHINode>(PN->use_back()))
9519 return DeadPHICycle(PU, PotentiallyDeadPHIs);
Misha Brukmanb1c93172005-04-21 23:48:37 +00009520
Chris Lattner71536432005-01-17 05:10:15 +00009521 return false;
9522}
9523
Chris Lattnerd8515f82007-11-06 21:52:06 +00009524/// PHIsEqualValue - Return true if this phi node is always equal to
9525/// NonPhiInVal. This happens with mutually cyclic phi nodes like:
9526/// z = some value; x = phi (y, z); y = phi (x, z)
9527static bool PHIsEqualValue(PHINode *PN, Value *NonPhiInVal,
9528 SmallPtrSet<PHINode*, 16> &ValueEqualPHIs) {
9529 // See if we already saw this PHI node.
9530 if (!ValueEqualPHIs.insert(PN))
9531 return true;
9532
9533 // Don't scan crazily complex things.
9534 if (ValueEqualPHIs.size() == 16)
9535 return false;
9536
9537 // Scan the operands to see if they are either phi nodes or are equal to
9538 // the value.
9539 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
9540 Value *Op = PN->getIncomingValue(i);
9541 if (PHINode *OpPN = dyn_cast<PHINode>(Op)) {
9542 if (!PHIsEqualValue(OpPN, NonPhiInVal, ValueEqualPHIs))
9543 return false;
9544 } else if (Op != NonPhiInVal)
9545 return false;
9546 }
9547
9548 return true;
9549}
9550
9551
Chris Lattnerbbbdd852002-05-06 18:06:38 +00009552// PHINode simplification
9553//
Chris Lattner113f4f42002-06-25 16:13:24 +00009554Instruction *InstCombiner::visitPHINode(PHINode &PN) {
Owen Andersonbbf89902006-07-10 22:15:25 +00009555 // If LCSSA is around, don't mess with Phi nodes
Chris Lattner8258b442007-03-04 04:27:24 +00009556 if (MustPreserveLCSSA) return 0;
Owen Andersona6968f82006-07-10 19:03:49 +00009557
Owen Andersonae8aa642006-07-10 22:03:18 +00009558 if (Value *V = PN.hasConstantValue())
9559 return ReplaceInstUsesWith(PN, V);
9560
Owen Andersonae8aa642006-07-10 22:03:18 +00009561 // If all PHI operands are the same operation, pull them through the PHI,
9562 // reducing code size.
9563 if (isa<Instruction>(PN.getIncomingValue(0)) &&
9564 PN.getIncomingValue(0)->hasOneUse())
9565 if (Instruction *Result = FoldPHIArgOpIntoPHI(PN))
9566 return Result;
9567
9568 // If this is a trivial cycle in the PHI node graph, remove it. Basically, if
9569 // this PHI only has a single use (a PHI), and if that PHI only has one use (a
9570 // PHI)... break the cycle.
Chris Lattnerc8dcede2007-01-15 07:30:06 +00009571 if (PN.hasOneUse()) {
9572 Instruction *PHIUser = cast<Instruction>(PN.use_back());
9573 if (PHINode *PU = dyn_cast<PHINode>(PHIUser)) {
Chris Lattnerd2602d52007-03-26 20:40:50 +00009574 SmallPtrSet<PHINode*, 16> PotentiallyDeadPHIs;
Owen Andersonae8aa642006-07-10 22:03:18 +00009575 PotentiallyDeadPHIs.insert(&PN);
9576 if (DeadPHICycle(PU, PotentiallyDeadPHIs))
9577 return ReplaceInstUsesWith(PN, UndefValue::get(PN.getType()));
9578 }
Chris Lattnerc8dcede2007-01-15 07:30:06 +00009579
9580 // If this phi has a single use, and if that use just computes a value for
9581 // the next iteration of a loop, delete the phi. This occurs with unused
9582 // induction variables, e.g. "for (int j = 0; ; ++j);". Detecting this
9583 // common case here is good because the only other things that catch this
9584 // are induction variable analysis (sometimes) and ADCE, which is only run
9585 // late.
9586 if (PHIUser->hasOneUse() &&
9587 (isa<BinaryOperator>(PHIUser) || isa<GetElementPtrInst>(PHIUser)) &&
9588 PHIUser->use_back() == &PN) {
9589 return ReplaceInstUsesWith(PN, UndefValue::get(PN.getType()));
9590 }
9591 }
Owen Andersonae8aa642006-07-10 22:03:18 +00009592
Chris Lattnerd8515f82007-11-06 21:52:06 +00009593 // We sometimes end up with phi cycles that non-obviously end up being the
9594 // same value, for example:
9595 // z = some value; x = phi (y, z); y = phi (x, z)
9596 // where the phi nodes don't necessarily need to be in the same block. Do a
9597 // quick check to see if the PHI node only contains a single non-phi value, if
9598 // so, scan to see if the phi cycle is actually equal to that value.
9599 {
9600 unsigned InValNo = 0, NumOperandVals = PN.getNumIncomingValues();
9601 // Scan for the first non-phi operand.
9602 while (InValNo != NumOperandVals &&
9603 isa<PHINode>(PN.getIncomingValue(InValNo)))
9604 ++InValNo;
9605
9606 if (InValNo != NumOperandVals) {
9607 Value *NonPhiInVal = PN.getOperand(InValNo);
9608
9609 // Scan the rest of the operands to see if there are any conflicts, if so
9610 // there is no need to recursively scan other phis.
9611 for (++InValNo; InValNo != NumOperandVals; ++InValNo) {
9612 Value *OpVal = PN.getIncomingValue(InValNo);
9613 if (OpVal != NonPhiInVal && !isa<PHINode>(OpVal))
9614 break;
9615 }
9616
9617 // If we scanned over all operands, then we have one unique value plus
9618 // phi values. Scan PHI nodes to see if they all merge in each other or
9619 // the value.
9620 if (InValNo == NumOperandVals) {
9621 SmallPtrSet<PHINode*, 16> ValueEqualPHIs;
9622 if (PHIsEqualValue(&PN, NonPhiInVal, ValueEqualPHIs))
9623 return ReplaceInstUsesWith(PN, NonPhiInVal);
9624 }
9625 }
9626 }
Chris Lattner91daeb52003-12-19 05:58:40 +00009627 return 0;
Chris Lattnerbbbdd852002-05-06 18:06:38 +00009628}
9629
Reid Spencer13bc5d72006-12-12 09:18:51 +00009630static Value *InsertCastToIntPtrTy(Value *V, const Type *DTy,
9631 Instruction *InsertPoint,
9632 InstCombiner *IC) {
Reid Spencer8f166b02007-01-08 16:32:00 +00009633 unsigned PtrSize = DTy->getPrimitiveSizeInBits();
9634 unsigned VTySize = V->getType()->getPrimitiveSizeInBits();
Reid Spencer13bc5d72006-12-12 09:18:51 +00009635 // We must cast correctly to the pointer type. Ensure that we
9636 // sign extend the integer value if it is smaller as this is
9637 // used for address computation.
9638 Instruction::CastOps opcode =
9639 (VTySize < PtrSize ? Instruction::SExt :
9640 (VTySize == PtrSize ? Instruction::BitCast : Instruction::Trunc));
9641 return IC->InsertCastBefore(opcode, V, DTy, *InsertPoint);
Chris Lattner69193f92004-04-05 01:30:19 +00009642}
9643
Chris Lattner48a44f72002-05-02 17:06:02 +00009644
Chris Lattner113f4f42002-06-25 16:13:24 +00009645Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) {
Chris Lattner5f667a62004-05-07 22:09:22 +00009646 Value *PtrOp = GEP.getOperand(0);
Chris Lattneracbf6a42007-04-28 00:57:34 +00009647 // Is it 'getelementptr %P, i32 0' or 'getelementptr %P'
Chris Lattner113f4f42002-06-25 16:13:24 +00009648 // If so, eliminate the noop.
Chris Lattner8d0bacb2004-02-22 05:25:17 +00009649 if (GEP.getNumOperands() == 1)
Chris Lattner5f667a62004-05-07 22:09:22 +00009650 return ReplaceInstUsesWith(GEP, PtrOp);
Chris Lattner8d0bacb2004-02-22 05:25:17 +00009651
Chris Lattner81a7a232004-10-16 18:11:37 +00009652 if (isa<UndefValue>(GEP.getOperand(0)))
9653 return ReplaceInstUsesWith(GEP, UndefValue::get(GEP.getType()));
9654
Chris Lattner8d0bacb2004-02-22 05:25:17 +00009655 bool HasZeroPointerIndex = false;
9656 if (Constant *C = dyn_cast<Constant>(GEP.getOperand(1)))
9657 HasZeroPointerIndex = C->isNullValue();
9658
9659 if (GEP.getNumOperands() == 2 && HasZeroPointerIndex)
Chris Lattner5f667a62004-05-07 22:09:22 +00009660 return ReplaceInstUsesWith(GEP, PtrOp);
Chris Lattner48a44f72002-05-02 17:06:02 +00009661
Chris Lattner69193f92004-04-05 01:30:19 +00009662 // Eliminate unneeded casts for indices.
9663 bool MadeChange = false;
Chris Lattner9bf53ff2007-03-25 20:43:09 +00009664
Chris Lattner2b2412d2004-04-07 18:38:20 +00009665 gep_type_iterator GTI = gep_type_begin(GEP);
Chris Lattner9bf53ff2007-03-25 20:43:09 +00009666 for (unsigned i = 1, e = GEP.getNumOperands(); i != e; ++i, ++GTI) {
Chris Lattner2b2412d2004-04-07 18:38:20 +00009667 if (isa<SequentialType>(*GTI)) {
9668 if (CastInst *CI = dyn_cast<CastInst>(GEP.getOperand(i))) {
Chris Lattner27df1db2007-01-15 07:02:54 +00009669 if (CI->getOpcode() == Instruction::ZExt ||
9670 CI->getOpcode() == Instruction::SExt) {
9671 const Type *SrcTy = CI->getOperand(0)->getType();
9672 // We can eliminate a cast from i32 to i64 iff the target
9673 // is a 32-bit pointer target.
9674 if (SrcTy->getPrimitiveSizeInBits() >= TD->getPointerSizeInBits()) {
9675 MadeChange = true;
9676 GEP.setOperand(i, CI->getOperand(0));
Chris Lattner69193f92004-04-05 01:30:19 +00009677 }
9678 }
9679 }
Chris Lattner2b2412d2004-04-07 18:38:20 +00009680 // If we are using a wider index than needed for this platform, shrink it
9681 // to what we need. If the incoming value needs a cast instruction,
9682 // insert it. This explicit cast can make subsequent optimizations more
9683 // obvious.
9684 Value *Op = GEP.getOperand(i);
Anton Korobeynikov1bfd1212008-02-20 11:26:25 +00009685 if (TD->getTypeSizeInBits(Op->getType()) > TD->getPointerSizeInBits()) {
Chris Lattner1e9ac1a2004-04-17 18:16:10 +00009686 if (Constant *C = dyn_cast<Constant>(Op)) {
Reid Spencer266e42b2006-12-23 06:05:41 +00009687 GEP.setOperand(i, ConstantExpr::getTrunc(C, TD->getIntPtrType()));
Chris Lattner1e9ac1a2004-04-17 18:16:10 +00009688 MadeChange = true;
9689 } else {
Reid Spencer13bc5d72006-12-12 09:18:51 +00009690 Op = InsertCastBefore(Instruction::Trunc, Op, TD->getIntPtrType(),
9691 GEP);
Chris Lattner2b2412d2004-04-07 18:38:20 +00009692 GEP.setOperand(i, Op);
9693 MadeChange = true;
9694 }
Anton Korobeynikov1bfd1212008-02-20 11:26:25 +00009695 }
Chris Lattner69193f92004-04-05 01:30:19 +00009696 }
Chris Lattner9bf53ff2007-03-25 20:43:09 +00009697 }
Chris Lattner69193f92004-04-05 01:30:19 +00009698 if (MadeChange) return &GEP;
9699
Chris Lattner9bf53ff2007-03-25 20:43:09 +00009700 // If this GEP instruction doesn't move the pointer, and if the input operand
9701 // is a bitcast of another pointer, just replace the GEP with a bitcast of the
9702 // real input to the dest type.
Chris Lattnerd8675e42007-10-12 05:30:59 +00009703 if (GEP.hasAllZeroIndices()) {
9704 if (BitCastInst *BCI = dyn_cast<BitCastInst>(GEP.getOperand(0))) {
9705 // If the bitcast is of an allocation, and the allocation will be
9706 // converted to match the type of the cast, don't touch this.
9707 if (isa<AllocationInst>(BCI->getOperand(0))) {
9708 // See if the bitcast simplifies, if so, don't nuke this GEP yet.
Chris Lattnerad618f62007-10-12 18:05:47 +00009709 if (Instruction *I = visitBitCast(*BCI)) {
9710 if (I != BCI) {
9711 I->takeName(BCI);
9712 BCI->getParent()->getInstList().insert(BCI, I);
9713 ReplaceInstUsesWith(*BCI, I);
9714 }
Chris Lattnerd8675e42007-10-12 05:30:59 +00009715 return &GEP;
Chris Lattnerad618f62007-10-12 18:05:47 +00009716 }
Chris Lattnerd8675e42007-10-12 05:30:59 +00009717 }
9718 return new BitCastInst(BCI->getOperand(0), GEP.getType());
9719 }
9720 }
9721
Chris Lattnerae7a0d32002-08-02 19:29:35 +00009722 // Combine Indices - If the source pointer to this getelementptr instruction
9723 // is a getelementptr instruction, combine the indices of the two
9724 // getelementptr instructions into a single instruction.
9725 //
Chris Lattneraf6094f2007-02-15 22:48:32 +00009726 SmallVector<Value*, 8> SrcGEPOperands;
Chris Lattner0798af32005-01-13 20:14:25 +00009727 if (User *Src = dyn_castGetElementPtr(PtrOp))
Chris Lattneraf6094f2007-02-15 22:48:32 +00009728 SrcGEPOperands.append(Src->op_begin(), Src->op_end());
Chris Lattner57c67b02004-03-25 22:59:29 +00009729
9730 if (!SrcGEPOperands.empty()) {
Chris Lattner5f667a62004-05-07 22:09:22 +00009731 // Note that if our source is a gep chain itself that we wait for that
9732 // chain to be resolved before we perform this transformation. This
9733 // avoids us creating a TON of code in some cases.
9734 //
9735 if (isa<GetElementPtrInst>(SrcGEPOperands[0]) &&
9736 cast<Instruction>(SrcGEPOperands[0])->getNumOperands() == 2)
9737 return 0; // Wait until our source is folded to completion.
9738
Chris Lattneraf6094f2007-02-15 22:48:32 +00009739 SmallVector<Value*, 8> Indices;
Chris Lattner5f667a62004-05-07 22:09:22 +00009740
9741 // Find out whether the last index in the source GEP is a sequential idx.
9742 bool EndsWithSequential = false;
9743 for (gep_type_iterator I = gep_type_begin(*cast<User>(PtrOp)),
9744 E = gep_type_end(*cast<User>(PtrOp)); I != E; ++I)
Chris Lattner8ec5f882004-05-08 22:41:42 +00009745 EndsWithSequential = !isa<StructType>(*I);
Misha Brukmanb1c93172005-04-21 23:48:37 +00009746
Chris Lattnerae7a0d32002-08-02 19:29:35 +00009747 // Can we combine the two pointer arithmetics offsets?
Chris Lattner5f667a62004-05-07 22:09:22 +00009748 if (EndsWithSequential) {
Chris Lattner235af562003-03-05 22:33:14 +00009749 // Replace: gep (gep %P, long B), long A, ...
9750 // With: T = long A+B; gep %P, T, ...
9751 //
Chris Lattner5f667a62004-05-07 22:09:22 +00009752 Value *Sum, *SO1 = SrcGEPOperands.back(), *GO1 = GEP.getOperand(1);
Chris Lattner69193f92004-04-05 01:30:19 +00009753 if (SO1 == Constant::getNullValue(SO1->getType())) {
9754 Sum = GO1;
9755 } else if (GO1 == Constant::getNullValue(GO1->getType())) {
9756 Sum = SO1;
9757 } else {
9758 // If they aren't the same type, convert both to an integer of the
9759 // target's pointer size.
9760 if (SO1->getType() != GO1->getType()) {
9761 if (Constant *SO1C = dyn_cast<Constant>(SO1)) {
Reid Spencer13bc5d72006-12-12 09:18:51 +00009762 SO1 = ConstantExpr::getIntegerCast(SO1C, GO1->getType(), true);
Chris Lattner69193f92004-04-05 01:30:19 +00009763 } else if (Constant *GO1C = dyn_cast<Constant>(GO1)) {
Reid Spencer13bc5d72006-12-12 09:18:51 +00009764 GO1 = ConstantExpr::getIntegerCast(GO1C, SO1->getType(), true);
Chris Lattner69193f92004-04-05 01:30:19 +00009765 } else {
Duncan Sands44b87212007-11-01 20:53:16 +00009766 unsigned PS = TD->getPointerSizeInBits();
9767 if (TD->getTypeSizeInBits(SO1->getType()) == PS) {
Chris Lattner69193f92004-04-05 01:30:19 +00009768 // Convert GO1 to SO1's type.
Reid Spencer13bc5d72006-12-12 09:18:51 +00009769 GO1 = InsertCastToIntPtrTy(GO1, SO1->getType(), &GEP, this);
Chris Lattner69193f92004-04-05 01:30:19 +00009770
Duncan Sands44b87212007-11-01 20:53:16 +00009771 } else if (TD->getTypeSizeInBits(GO1->getType()) == PS) {
Chris Lattner69193f92004-04-05 01:30:19 +00009772 // Convert SO1 to GO1's type.
Reid Spencer13bc5d72006-12-12 09:18:51 +00009773 SO1 = InsertCastToIntPtrTy(SO1, GO1->getType(), &GEP, this);
Chris Lattner69193f92004-04-05 01:30:19 +00009774 } else {
9775 const Type *PT = TD->getIntPtrType();
Reid Spencer13bc5d72006-12-12 09:18:51 +00009776 SO1 = InsertCastToIntPtrTy(SO1, PT, &GEP, this);
9777 GO1 = InsertCastToIntPtrTy(GO1, PT, &GEP, this);
Chris Lattner69193f92004-04-05 01:30:19 +00009778 }
9779 }
9780 }
Chris Lattner5f667a62004-05-07 22:09:22 +00009781 if (isa<Constant>(SO1) && isa<Constant>(GO1))
9782 Sum = ConstantExpr::getAdd(cast<Constant>(SO1), cast<Constant>(GO1));
9783 else {
Chris Lattnerdf20a4d2004-06-10 02:07:29 +00009784 Sum = BinaryOperator::createAdd(SO1, GO1, PtrOp->getName()+".sum");
9785 InsertNewInstBefore(cast<Instruction>(Sum), GEP);
Chris Lattner5f667a62004-05-07 22:09:22 +00009786 }
Chris Lattner69193f92004-04-05 01:30:19 +00009787 }
Chris Lattner5f667a62004-05-07 22:09:22 +00009788
9789 // Recycle the GEP we already have if possible.
9790 if (SrcGEPOperands.size() == 2) {
9791 GEP.setOperand(0, SrcGEPOperands[0]);
9792 GEP.setOperand(1, Sum);
9793 return &GEP;
9794 } else {
9795 Indices.insert(Indices.end(), SrcGEPOperands.begin()+1,
9796 SrcGEPOperands.end()-1);
9797 Indices.push_back(Sum);
9798 Indices.insert(Indices.end(), GEP.op_begin()+2, GEP.op_end());
9799 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00009800 } else if (isa<Constant>(*GEP.idx_begin()) &&
Chris Lattner69193f92004-04-05 01:30:19 +00009801 cast<Constant>(*GEP.idx_begin())->isNullValue() &&
Misha Brukmanb1c93172005-04-21 23:48:37 +00009802 SrcGEPOperands.size() != 1) {
Chris Lattnerae7a0d32002-08-02 19:29:35 +00009803 // Otherwise we can do the fold if the first index of the GEP is a zero
Chris Lattner57c67b02004-03-25 22:59:29 +00009804 Indices.insert(Indices.end(), SrcGEPOperands.begin()+1,
9805 SrcGEPOperands.end());
Chris Lattnerae7a0d32002-08-02 19:29:35 +00009806 Indices.insert(Indices.end(), GEP.idx_begin()+1, GEP.idx_end());
9807 }
9808
9809 if (!Indices.empty())
Gabor Greife9ecc682008-04-06 20:25:17 +00009810 return GetElementPtrInst::Create(SrcGEPOperands[0], Indices.begin(),
9811 Indices.end(), GEP.getName());
Chris Lattnerc59af1d2002-08-17 22:21:59 +00009812
Chris Lattner5f667a62004-05-07 22:09:22 +00009813 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(PtrOp)) {
Chris Lattnerc59af1d2002-08-17 22:21:59 +00009814 // GEP of global variable. If all of the indices for this GEP are
9815 // constants, we can promote this to a constexpr instead of an instruction.
9816
9817 // Scan for nonconstants...
Chris Lattnerf96f4a82007-01-31 04:40:53 +00009818 SmallVector<Constant*, 8> Indices;
Chris Lattnerc59af1d2002-08-17 22:21:59 +00009819 User::op_iterator I = GEP.idx_begin(), E = GEP.idx_end();
9820 for (; I != E && isa<Constant>(*I); ++I)
9821 Indices.push_back(cast<Constant>(*I));
9822
9823 if (I == E) { // If they are all constants...
Chris Lattnerf96f4a82007-01-31 04:40:53 +00009824 Constant *CE = ConstantExpr::getGetElementPtr(GV,
9825 &Indices[0],Indices.size());
Chris Lattnerc59af1d2002-08-17 22:21:59 +00009826
9827 // Replace all uses of the GEP with the new constexpr...
9828 return ReplaceInstUsesWith(GEP, CE);
9829 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00009830 } else if (Value *X = getBitCastOperand(PtrOp)) { // Is the operand a cast?
Chris Lattner567b81f2005-09-13 00:40:14 +00009831 if (!isa<PointerType>(X->getType())) {
9832 // Not interesting. Source pointer must be a cast from pointer.
9833 } else if (HasZeroPointerIndex) {
Wojciech Matyjewicz309e5a72007-12-12 15:21:32 +00009834 // transform: GEP (bitcast [10 x i8]* X to [0 x i8]*), i32 0, ...
9835 // into : GEP [10 x i8]* X, i32 0, ...
Chris Lattner567b81f2005-09-13 00:40:14 +00009836 //
9837 // This occurs when the program declares an array extern like "int X[];"
9838 //
9839 const PointerType *CPTy = cast<PointerType>(PtrOp->getType());
9840 const PointerType *XTy = cast<PointerType>(X->getType());
9841 if (const ArrayType *XATy =
9842 dyn_cast<ArrayType>(XTy->getElementType()))
9843 if (const ArrayType *CATy =
9844 dyn_cast<ArrayType>(CPTy->getElementType()))
9845 if (CATy->getElementType() == XATy->getElementType()) {
9846 // At this point, we know that the cast source type is a pointer
9847 // to an array of the same type as the destination pointer
9848 // array. Because the array type is never stepped over (there
9849 // is a leading zero) we can fold the cast into this GEP.
9850 GEP.setOperand(0, X);
9851 return &GEP;
9852 }
9853 } else if (GEP.getNumOperands() == 2) {
9854 // Transform things like:
Wojciech Matyjewicz309e5a72007-12-12 15:21:32 +00009855 // %t = getelementptr i32* bitcast ([2 x i32]* %str to i32*), i32 %V
9856 // into: %t1 = getelementptr [2 x i32]* %str, i32 0, i32 %V; bitcast
Chris Lattner567b81f2005-09-13 00:40:14 +00009857 const Type *SrcElTy = cast<PointerType>(X->getType())->getElementType();
9858 const Type *ResElTy=cast<PointerType>(PtrOp->getType())->getElementType();
9859 if (isa<ArrayType>(SrcElTy) &&
Duncan Sands44b87212007-11-01 20:53:16 +00009860 TD->getABITypeSize(cast<ArrayType>(SrcElTy)->getElementType()) ==
9861 TD->getABITypeSize(ResElTy)) {
David Greenec656cbb2007-09-04 15:46:09 +00009862 Value *Idx[2];
9863 Idx[0] = Constant::getNullValue(Type::Int32Ty);
9864 Idx[1] = GEP.getOperand(1);
Chris Lattner567b81f2005-09-13 00:40:14 +00009865 Value *V = InsertNewInstBefore(
Gabor Greife9ecc682008-04-06 20:25:17 +00009866 GetElementPtrInst::Create(X, Idx, Idx + 2, GEP.getName()), GEP);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00009867 // V and GEP are both pointer types --> BitCast
9868 return new BitCastInst(V, GEP.getType());
Chris Lattner8d0bacb2004-02-22 05:25:17 +00009869 }
Chris Lattner2a893292005-09-13 18:36:04 +00009870
9871 // Transform things like:
Wojciech Matyjewicz309e5a72007-12-12 15:21:32 +00009872 // getelementptr i8* bitcast ([100 x double]* X to i8*), i32 %tmp
Chris Lattner2a893292005-09-13 18:36:04 +00009873 // (where tmp = 8*tmp2) into:
Wojciech Matyjewicz309e5a72007-12-12 15:21:32 +00009874 // getelementptr [100 x double]* %arr, i32 0, i32 %tmp2; bitcast
Chris Lattner2a893292005-09-13 18:36:04 +00009875
Wojciech Matyjewicz309e5a72007-12-12 15:21:32 +00009876 if (isa<ArrayType>(SrcElTy) && ResElTy == Type::Int8Ty) {
Chris Lattner2a893292005-09-13 18:36:04 +00009877 uint64_t ArrayEltSize =
Duncan Sands44b87212007-11-01 20:53:16 +00009878 TD->getABITypeSize(cast<ArrayType>(SrcElTy)->getElementType());
Chris Lattner2a893292005-09-13 18:36:04 +00009879
9880 // Check to see if "tmp" is a scale by a multiple of ArrayEltSize. We
9881 // allow either a mul, shift, or constant here.
9882 Value *NewIdx = 0;
9883 ConstantInt *Scale = 0;
9884 if (ArrayEltSize == 1) {
9885 NewIdx = GEP.getOperand(1);
9886 Scale = ConstantInt::get(NewIdx->getType(), 1);
9887 } else if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP.getOperand(1))) {
Chris Lattnera393e4d2005-09-14 17:32:56 +00009888 NewIdx = ConstantInt::get(CI->getType(), 1);
Chris Lattner2a893292005-09-13 18:36:04 +00009889 Scale = CI;
9890 } else if (Instruction *Inst =dyn_cast<Instruction>(GEP.getOperand(1))){
9891 if (Inst->getOpcode() == Instruction::Shl &&
9892 isa<ConstantInt>(Inst->getOperand(1))) {
Zhou Shengb25806f2007-03-30 09:29:48 +00009893 ConstantInt *ShAmt = cast<ConstantInt>(Inst->getOperand(1));
9894 uint32_t ShAmtVal = ShAmt->getLimitedValue(64);
9895 Scale = ConstantInt::get(Inst->getType(), 1ULL << ShAmtVal);
Chris Lattner2a893292005-09-13 18:36:04 +00009896 NewIdx = Inst->getOperand(0);
9897 } else if (Inst->getOpcode() == Instruction::Mul &&
9898 isa<ConstantInt>(Inst->getOperand(1))) {
9899 Scale = cast<ConstantInt>(Inst->getOperand(1));
9900 NewIdx = Inst->getOperand(0);
9901 }
9902 }
Wojciech Matyjewicz309e5a72007-12-12 15:21:32 +00009903
Chris Lattner2a893292005-09-13 18:36:04 +00009904 // If the index will be to exactly the right offset with the scale taken
Wojciech Matyjewicz309e5a72007-12-12 15:21:32 +00009905 // out, perform the transformation. Note, we don't know whether Scale is
9906 // signed or not. We'll use unsigned version of division/modulo
9907 // operation after making sure Scale doesn't have the sign bit set.
9908 if (Scale && Scale->getSExtValue() >= 0LL &&
9909 Scale->getZExtValue() % ArrayEltSize == 0) {
9910 Scale = ConstantInt::get(Scale->getType(),
9911 Scale->getZExtValue() / ArrayEltSize);
Reid Spencere0fc4df2006-10-20 07:07:24 +00009912 if (Scale->getZExtValue() != 1) {
Reid Spencer13bc5d72006-12-12 09:18:51 +00009913 Constant *C = ConstantExpr::getIntegerCast(Scale, NewIdx->getType(),
Wojciech Matyjewicz309e5a72007-12-12 15:21:32 +00009914 false /*ZExt*/);
Chris Lattner2a893292005-09-13 18:36:04 +00009915 Instruction *Sc = BinaryOperator::createMul(NewIdx, C, "idxscale");
9916 NewIdx = InsertNewInstBefore(Sc, GEP);
9917 }
9918
9919 // Insert the new GEP instruction.
David Greenec656cbb2007-09-04 15:46:09 +00009920 Value *Idx[2];
9921 Idx[0] = Constant::getNullValue(Type::Int32Ty);
9922 Idx[1] = NewIdx;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00009923 Instruction *NewGEP =
Gabor Greife9ecc682008-04-06 20:25:17 +00009924 GetElementPtrInst::Create(X, Idx, Idx + 2, GEP.getName());
Reid Spencer6c38f0b2006-11-27 01:05:10 +00009925 NewGEP = InsertNewInstBefore(NewGEP, GEP);
9926 // The NewGEP must be pointer typed, so must the old one -> BitCast
9927 return new BitCastInst(NewGEP, GEP.getType());
Chris Lattner2a893292005-09-13 18:36:04 +00009928 }
9929 }
Chris Lattner8d0bacb2004-02-22 05:25:17 +00009930 }
Chris Lattnerca081252001-12-14 16:52:21 +00009931 }
9932
Chris Lattnerca081252001-12-14 16:52:21 +00009933 return 0;
9934}
9935
Chris Lattner1085bdf2002-11-04 16:18:53 +00009936Instruction *InstCombiner::visitAllocationInst(AllocationInst &AI) {
9937 // Convert: malloc Ty, C - where C is a constant != 1 into: malloc [C x Ty], 1
Anton Korobeynikov1bfd1212008-02-20 11:26:25 +00009938 if (AI.isArrayAllocation()) { // Check C != 1
Reid Spencere0fc4df2006-10-20 07:07:24 +00009939 if (const ConstantInt *C = dyn_cast<ConstantInt>(AI.getArraySize())) {
9940 const Type *NewTy =
9941 ArrayType::get(AI.getAllocatedType(), C->getZExtValue());
Chris Lattnera2620ac2002-11-09 00:49:43 +00009942 AllocationInst *New = 0;
Chris Lattner1085bdf2002-11-04 16:18:53 +00009943
9944 // Create and insert the replacement instruction...
9945 if (isa<MallocInst>(AI))
Nate Begeman848622f2005-11-05 09:21:28 +00009946 New = new MallocInst(NewTy, 0, AI.getAlignment(), AI.getName());
Chris Lattnera2620ac2002-11-09 00:49:43 +00009947 else {
9948 assert(isa<AllocaInst>(AI) && "Unknown type of allocation inst!");
Nate Begeman848622f2005-11-05 09:21:28 +00009949 New = new AllocaInst(NewTy, 0, AI.getAlignment(), AI.getName());
Chris Lattnera2620ac2002-11-09 00:49:43 +00009950 }
Chris Lattnerabb77c92004-03-19 06:08:10 +00009951
9952 InsertNewInstBefore(New, AI);
Misha Brukmanb1c93172005-04-21 23:48:37 +00009953
Chris Lattner1085bdf2002-11-04 16:18:53 +00009954 // Scan to the end of the allocation instructions, to skip over a block of
9955 // allocas if possible...
9956 //
9957 BasicBlock::iterator It = New;
9958 while (isa<AllocationInst>(*It)) ++It;
9959
9960 // Now that I is pointing to the first non-allocation-inst in the block,
9961 // insert our getelementptr instruction...
9962 //
Reid Spencerc635f472006-12-31 05:48:39 +00009963 Value *NullIdx = Constant::getNullValue(Type::Int32Ty);
David Greenec656cbb2007-09-04 15:46:09 +00009964 Value *Idx[2];
9965 Idx[0] = NullIdx;
9966 Idx[1] = NullIdx;
Gabor Greife9ecc682008-04-06 20:25:17 +00009967 Value *V = GetElementPtrInst::Create(New, Idx, Idx + 2,
9968 New->getName()+".sub", It);
Chris Lattner1085bdf2002-11-04 16:18:53 +00009969
9970 // Now make everything use the getelementptr instead of the original
9971 // allocation.
Chris Lattnerabb77c92004-03-19 06:08:10 +00009972 return ReplaceInstUsesWith(AI, V);
Chris Lattner81a7a232004-10-16 18:11:37 +00009973 } else if (isa<UndefValue>(AI.getArraySize())) {
9974 return ReplaceInstUsesWith(AI, Constant::getNullValue(AI.getType()));
Chris Lattner1085bdf2002-11-04 16:18:53 +00009975 }
Anton Korobeynikov1bfd1212008-02-20 11:26:25 +00009976 }
Chris Lattnerabb77c92004-03-19 06:08:10 +00009977
9978 // If alloca'ing a zero byte object, replace the alloca with a null pointer.
9979 // Note that we only do this for alloca's, because malloc should allocate and
9980 // return a unique pointer, even for a zero byte allocation.
Misha Brukmanb1c93172005-04-21 23:48:37 +00009981 if (isa<AllocaInst>(AI) && AI.getAllocatedType()->isSized() &&
Duncan Sands44b87212007-11-01 20:53:16 +00009982 TD->getABITypeSize(AI.getAllocatedType()) == 0)
Chris Lattnerabb77c92004-03-19 06:08:10 +00009983 return ReplaceInstUsesWith(AI, Constant::getNullValue(AI.getType()));
9984
Chris Lattner1085bdf2002-11-04 16:18:53 +00009985 return 0;
9986}
9987
Chris Lattner8427bff2003-12-07 01:24:23 +00009988Instruction *InstCombiner::visitFreeInst(FreeInst &FI) {
9989 Value *Op = FI.getOperand(0);
9990
Chris Lattner8ba9ec92004-10-18 02:59:09 +00009991 // free undef -> unreachable.
9992 if (isa<UndefValue>(Op)) {
9993 // Insert a new store to null because we cannot modify the CFG here.
Zhou Sheng75b871f2007-01-11 12:24:14 +00009994 new StoreInst(ConstantInt::getTrue(),
Christopher Lambedf07882007-12-17 01:12:55 +00009995 UndefValue::get(PointerType::getUnqual(Type::Int1Ty)), &FI);
Chris Lattner8ba9ec92004-10-18 02:59:09 +00009996 return EraseInstFromFunction(FI);
9997 }
Chris Lattnerefb33d22007-04-14 00:20:02 +00009998
Chris Lattnerf3a36602004-02-28 04:57:37 +00009999 // If we have 'free null' delete the instruction. This can happen in stl code
10000 // when lots of inlining happens.
Chris Lattner8ba9ec92004-10-18 02:59:09 +000010001 if (isa<ConstantPointerNull>(Op))
Chris Lattner51ea1272004-02-28 05:22:00 +000010002 return EraseInstFromFunction(FI);
Chris Lattnerefb33d22007-04-14 00:20:02 +000010003
10004 // Change free <ty>* (cast <ty2>* X to <ty>*) into free <ty2>* X
10005 if (BitCastInst *CI = dyn_cast<BitCastInst>(Op)) {
10006 FI.setOperand(0, CI->getOperand(0));
10007 return &FI;
10008 }
10009
10010 // Change free (gep X, 0,0,0,0) into free(X)
10011 if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(Op)) {
10012 if (GEPI->hasAllZeroIndices()) {
10013 AddToWorkList(GEPI);
10014 FI.setOperand(0, GEPI->getOperand(0));
10015 return &FI;
10016 }
10017 }
10018
10019 // Change free(malloc) into nothing, if the malloc has a single use.
10020 if (MallocInst *MI = dyn_cast<MallocInst>(Op))
10021 if (MI->hasOneUse()) {
10022 EraseInstFromFunction(FI);
10023 return EraseInstFromFunction(*MI);
10024 }
Chris Lattnerf3a36602004-02-28 04:57:37 +000010025
Chris Lattner8427bff2003-12-07 01:24:23 +000010026 return 0;
10027}
10028
10029
Chris Lattner72684fe2005-01-31 05:51:45 +000010030/// InstCombineLoadCast - Fold 'load (cast P)' -> cast (load P)' when possible.
Devang Pateldf49cf52007-10-18 19:52:32 +000010031static Instruction *InstCombineLoadCast(InstCombiner &IC, LoadInst &LI,
Bill Wendlingd188e032008-02-26 10:53:30 +000010032 const TargetData *TD) {
Chris Lattner35e24772004-07-13 01:49:43 +000010033 User *CI = cast<User>(LI.getOperand(0));
Chris Lattnerfe1b0b82005-01-31 04:50:46 +000010034 Value *CastOp = CI->getOperand(0);
Chris Lattner35e24772004-07-13 01:49:43 +000010035
Devang Pateldf49cf52007-10-18 19:52:32 +000010036 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(CI)) {
10037 // Instead of loading constant c string, use corresponding integer value
10038 // directly if string length is small enough.
10039 const std::string &Str = CE->getOperand(0)->getStringValue();
10040 if (!Str.empty()) {
10041 unsigned len = Str.length();
10042 const Type *Ty = cast<PointerType>(CE->getType())->getElementType();
10043 unsigned numBits = Ty->getPrimitiveSizeInBits();
10044 // Replace LI with immediate integer store.
10045 if ((numBits >> 3) == len + 1) {
Bill Wendlingd188e032008-02-26 10:53:30 +000010046 APInt StrVal(numBits, 0);
10047 APInt SingleChar(numBits, 0);
10048 if (TD->isLittleEndian()) {
10049 for (signed i = len-1; i >= 0; i--) {
10050 SingleChar = (uint64_t) Str[i];
10051 StrVal = (StrVal << 8) | SingleChar;
10052 }
10053 } else {
10054 for (unsigned i = 0; i < len; i++) {
10055 SingleChar = (uint64_t) Str[i];
10056 StrVal = (StrVal << 8) | SingleChar;
10057 }
10058 // Append NULL at the end.
10059 SingleChar = 0;
10060 StrVal = (StrVal << 8) | SingleChar;
10061 }
10062 Value *NL = ConstantInt::get(StrVal);
10063 return IC.ReplaceInstUsesWith(LI, NL);
Devang Pateldf49cf52007-10-18 19:52:32 +000010064 }
10065 }
10066 }
10067
Chris Lattner35e24772004-07-13 01:49:43 +000010068 const Type *DestPTy = cast<PointerType>(CI->getType())->getElementType();
Chris Lattnerfe1b0b82005-01-31 04:50:46 +000010069 if (const PointerType *SrcTy = dyn_cast<PointerType>(CastOp->getType())) {
Chris Lattner35e24772004-07-13 01:49:43 +000010070 const Type *SrcPTy = SrcTy->getElementType();
Chris Lattnerfe1b0b82005-01-31 04:50:46 +000010071
Reid Spencer31a4ef42007-01-22 05:51:25 +000010072 if (DestPTy->isInteger() || isa<PointerType>(DestPTy) ||
Reid Spencerd84d35b2007-02-15 02:26:10 +000010073 isa<VectorType>(DestPTy)) {
Chris Lattnerfe1b0b82005-01-31 04:50:46 +000010074 // If the source is an array, the code below will not succeed. Check to
10075 // see if a trivial 'gep P, 0, 0' will help matters. Only do this for
10076 // constants.
10077 if (const ArrayType *ASrcTy = dyn_cast<ArrayType>(SrcPTy))
10078 if (Constant *CSrc = dyn_cast<Constant>(CastOp))
10079 if (ASrcTy->getNumElements() != 0) {
Chris Lattnerf96f4a82007-01-31 04:40:53 +000010080 Value *Idxs[2];
10081 Idxs[0] = Idxs[1] = Constant::getNullValue(Type::Int32Ty);
10082 CastOp = ConstantExpr::getGetElementPtr(CSrc, Idxs, 2);
Chris Lattnerfe1b0b82005-01-31 04:50:46 +000010083 SrcTy = cast<PointerType>(CastOp->getType());
10084 SrcPTy = SrcTy->getElementType();
10085 }
10086
Reid Spencer31a4ef42007-01-22 05:51:25 +000010087 if ((SrcPTy->isInteger() || isa<PointerType>(SrcPTy) ||
Reid Spencerd84d35b2007-02-15 02:26:10 +000010088 isa<VectorType>(SrcPTy)) &&
Chris Lattnerecfa9b52005-03-29 06:37:47 +000010089 // Do not allow turning this into a load of an integer, which is then
10090 // casted to a pointer, this pessimizes pointer analysis a lot.
10091 (isa<PointerType>(SrcPTy) == isa<PointerType>(LI.getType())) &&
Reid Spencer31a4ef42007-01-22 05:51:25 +000010092 IC.getTargetData().getTypeSizeInBits(SrcPTy) ==
10093 IC.getTargetData().getTypeSizeInBits(DestPTy)) {
Misha Brukmanb1c93172005-04-21 23:48:37 +000010094
Chris Lattnerfe1b0b82005-01-31 04:50:46 +000010095 // Okay, we are casting from one integer or pointer type to another of
10096 // the same size. Instead of casting the pointer before the load, cast
10097 // the result of the loaded value.
10098 Value *NewLoad = IC.InsertNewInstBefore(new LoadInst(CastOp,
10099 CI->getName(),
10100 LI.isVolatile()),LI);
10101 // Now cast the result of the load.
Reid Spencerbb65ebf2006-12-12 23:36:14 +000010102 return new BitCastInst(NewLoad, LI.getType());
Chris Lattnerfe1b0b82005-01-31 04:50:46 +000010103 }
Chris Lattner35e24772004-07-13 01:49:43 +000010104 }
10105 }
10106 return 0;
10107}
10108
Chris Lattnerf62ea8e2004-09-19 18:43:46 +000010109/// isSafeToLoadUnconditionally - Return true if we know that executing a load
Chris Lattnere6f13092004-09-19 19:18:10 +000010110/// from this value cannot trap. If it is not obviously safe to load from the
10111/// specified pointer, we do a quick local scan of the basic block containing
10112/// ScanFrom, to determine if the address is already accessed.
10113static bool isSafeToLoadUnconditionally(Value *V, Instruction *ScanFrom) {
Duncan Sands56df7de2007-09-19 10:10:31 +000010114 // If it is an alloca it is always safe to load from.
10115 if (isa<AllocaInst>(V)) return true;
10116
Duncan Sandsd31649b2007-09-19 10:25:38 +000010117 // If it is a global variable it is mostly safe to load from.
Duncan Sands56df7de2007-09-19 10:10:31 +000010118 if (const GlobalValue *GV = dyn_cast<GlobalVariable>(V))
Duncan Sandsd31649b2007-09-19 10:25:38 +000010119 // Don't try to evaluate aliases. External weak GV can be null.
Duncan Sands56df7de2007-09-19 10:10:31 +000010120 return !isa<GlobalAlias>(GV) && !GV->hasExternalWeakLinkage();
Chris Lattnere6f13092004-09-19 19:18:10 +000010121
10122 // Otherwise, be a little bit agressive by scanning the local block where we
10123 // want to check to see if the pointer is already being loaded or stored
Alkis Evlogimenosd59cebf2004-09-20 06:42:58 +000010124 // from/to. If so, the previous load or store would have already trapped,
10125 // so there is no harm doing an extra load (also, CSE will later eliminate
10126 // the load entirely).
Chris Lattnere6f13092004-09-19 19:18:10 +000010127 BasicBlock::iterator BBI = ScanFrom, E = ScanFrom->getParent()->begin();
10128
Alkis Evlogimenosd59cebf2004-09-20 06:42:58 +000010129 while (BBI != E) {
Chris Lattnere6f13092004-09-19 19:18:10 +000010130 --BBI;
10131
10132 if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) {
10133 if (LI->getOperand(0) == V) return true;
10134 } else if (StoreInst *SI = dyn_cast<StoreInst>(BBI))
10135 if (SI->getOperand(1) == V) return true;
Misha Brukmanb1c93172005-04-21 23:48:37 +000010136
Alkis Evlogimenosd59cebf2004-09-20 06:42:58 +000010137 }
Chris Lattnere6f13092004-09-19 19:18:10 +000010138 return false;
Chris Lattnerf62ea8e2004-09-19 18:43:46 +000010139}
10140
Chris Lattner99c8ee22007-08-11 18:48:48 +000010141/// GetUnderlyingObject - Trace through a series of getelementptrs and bitcasts
10142/// until we find the underlying object a pointer is referring to or something
10143/// we don't understand. Note that the returned pointer may be offset from the
10144/// input, because we ignore GEP indices.
10145static Value *GetUnderlyingObject(Value *Ptr) {
10146 while (1) {
10147 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) {
10148 if (CE->getOpcode() == Instruction::BitCast ||
10149 CE->getOpcode() == Instruction::GetElementPtr)
10150 Ptr = CE->getOperand(0);
10151 else
10152 return Ptr;
10153 } else if (BitCastInst *BCI = dyn_cast<BitCastInst>(Ptr)) {
10154 Ptr = BCI->getOperand(0);
10155 } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr)) {
10156 Ptr = GEP->getOperand(0);
10157 } else {
10158 return Ptr;
10159 }
10160 }
10161}
10162
Chris Lattner0f1d8a32003-06-26 05:06:25 +000010163Instruction *InstCombiner::visitLoadInst(LoadInst &LI) {
10164 Value *Op = LI.getOperand(0);
Chris Lattner7e8af382004-01-12 04:13:56 +000010165
Dan Gohmane31a61e2007-07-20 16:34:21 +000010166 // Attempt to improve the alignment.
Dan Gohman99b7b3f2008-04-10 18:43:06 +000010167 unsigned KnownAlign = GetOrEnforceKnownAlignment(Op);
10168 if (KnownAlign >
10169 (LI.getAlignment() == 0 ? TD->getABITypeAlignment(LI.getType()) :
10170 LI.getAlignment()))
Dan Gohmane31a61e2007-07-20 16:34:21 +000010171 LI.setAlignment(KnownAlign);
10172
Chris Lattnera9d84e32005-05-01 04:24:53 +000010173 // load (cast X) --> cast (load X) iff safe
Reid Spencerde46e482006-11-02 20:25:50 +000010174 if (isa<CastInst>(Op))
Devang Pateldf49cf52007-10-18 19:52:32 +000010175 if (Instruction *Res = InstCombineLoadCast(*this, LI, TD))
Chris Lattnera9d84e32005-05-01 04:24:53 +000010176 return Res;
10177
10178 // None of the following transforms are legal for volatile loads.
10179 if (LI.isVolatile()) return 0;
Chris Lattnerb990f7d2005-09-12 22:00:15 +000010180
Chris Lattnerb990f7d2005-09-12 22:00:15 +000010181 if (&LI.getParent()->front() != &LI) {
10182 BasicBlock::iterator BBI = &LI; --BBI;
Chris Lattnere0bfdf12005-09-12 22:21:03 +000010183 // If the instruction immediately before this is a store to the same
10184 // address, do a simple form of store->load forwarding.
Chris Lattnerb990f7d2005-09-12 22:00:15 +000010185 if (StoreInst *SI = dyn_cast<StoreInst>(BBI))
10186 if (SI->getOperand(1) == LI.getOperand(0))
10187 return ReplaceInstUsesWith(LI, SI->getOperand(0));
Chris Lattnere0bfdf12005-09-12 22:21:03 +000010188 if (LoadInst *LIB = dyn_cast<LoadInst>(BBI))
10189 if (LIB->getOperand(0) == LI.getOperand(0))
10190 return ReplaceInstUsesWith(LI, LIB);
Chris Lattnerb990f7d2005-09-12 22:00:15 +000010191 }
Chris Lattnera9d84e32005-05-01 04:24:53 +000010192
Christopher Lambb053b802007-12-29 07:56:53 +000010193 if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(Op)) {
10194 const Value *GEPI0 = GEPI->getOperand(0);
10195 // TODO: Consider a target hook for valid address spaces for this xform.
10196 if (isa<ConstantPointerNull>(GEPI0) &&
10197 cast<PointerType>(GEPI0->getType())->getAddressSpace() == 0) {
Chris Lattnera9d84e32005-05-01 04:24:53 +000010198 // Insert a new store to null instruction before the load to indicate
10199 // that this code is not reachable. We do this instead of inserting
10200 // an unreachable instruction directly because we cannot modify the
10201 // CFG.
10202 new StoreInst(UndefValue::get(LI.getType()),
10203 Constant::getNullValue(Op->getType()), &LI);
10204 return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType()));
10205 }
Christopher Lambb053b802007-12-29 07:56:53 +000010206 }
Chris Lattnera9d84e32005-05-01 04:24:53 +000010207
Chris Lattner81a7a232004-10-16 18:11:37 +000010208 if (Constant *C = dyn_cast<Constant>(Op)) {
Chris Lattnera9d84e32005-05-01 04:24:53 +000010209 // load null/undef -> undef
Christopher Lambb053b802007-12-29 07:56:53 +000010210 // TODO: Consider a target hook for valid address spaces for this xform.
10211 if (isa<UndefValue>(C) || (C->isNullValue() &&
10212 cast<PointerType>(Op->getType())->getAddressSpace() == 0)) {
Chris Lattner8ba9ec92004-10-18 02:59:09 +000010213 // Insert a new store to null instruction before the load to indicate that
10214 // this code is not reachable. We do this instead of inserting an
10215 // unreachable instruction directly because we cannot modify the CFG.
Chris Lattnera9d84e32005-05-01 04:24:53 +000010216 new StoreInst(UndefValue::get(LI.getType()),
10217 Constant::getNullValue(Op->getType()), &LI);
Chris Lattner81a7a232004-10-16 18:11:37 +000010218 return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType()));
Chris Lattner8ba9ec92004-10-18 02:59:09 +000010219 }
Chris Lattner0f1d8a32003-06-26 05:06:25 +000010220
Chris Lattner81a7a232004-10-16 18:11:37 +000010221 // Instcombine load (constant global) into the value loaded.
10222 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Op))
Reid Spencer5301e7c2007-01-30 20:08:39 +000010223 if (GV->isConstant() && !GV->isDeclaration())
Chris Lattner81a7a232004-10-16 18:11:37 +000010224 return ReplaceInstUsesWith(LI, GV->getInitializer());
Misha Brukmanb1c93172005-04-21 23:48:37 +000010225
Chris Lattner81a7a232004-10-16 18:11:37 +000010226 // Instcombine load (constantexpr_GEP global, 0, ...) into the value loaded.
Anton Korobeynikov1bfd1212008-02-20 11:26:25 +000010227 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Op)) {
Chris Lattner81a7a232004-10-16 18:11:37 +000010228 if (CE->getOpcode() == Instruction::GetElementPtr) {
10229 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(CE->getOperand(0)))
Reid Spencer5301e7c2007-01-30 20:08:39 +000010230 if (GV->isConstant() && !GV->isDeclaration())
Chris Lattner0b011ec2005-09-26 05:28:06 +000010231 if (Constant *V =
10232 ConstantFoldLoadThroughGEPConstantExpr(GV->getInitializer(), CE))
Chris Lattner81a7a232004-10-16 18:11:37 +000010233 return ReplaceInstUsesWith(LI, V);
Chris Lattnera9d84e32005-05-01 04:24:53 +000010234 if (CE->getOperand(0)->isNullValue()) {
10235 // Insert a new store to null instruction before the load to indicate
10236 // that this code is not reachable. We do this instead of inserting
10237 // an unreachable instruction directly because we cannot modify the
10238 // CFG.
10239 new StoreInst(UndefValue::get(LI.getType()),
10240 Constant::getNullValue(Op->getType()), &LI);
10241 return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType()));
10242 }
10243
Reid Spencer6c38f0b2006-11-27 01:05:10 +000010244 } else if (CE->isCast()) {
Devang Pateldf49cf52007-10-18 19:52:32 +000010245 if (Instruction *Res = InstCombineLoadCast(*this, LI, TD))
Chris Lattner81a7a232004-10-16 18:11:37 +000010246 return Res;
10247 }
Anton Korobeynikov1bfd1212008-02-20 11:26:25 +000010248 }
Chris Lattner81a7a232004-10-16 18:11:37 +000010249 }
Chris Lattner99c8ee22007-08-11 18:48:48 +000010250
10251 // If this load comes from anywhere in a constant global, and if the global
10252 // is all undef or zero, we know what it loads.
10253 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(GetUnderlyingObject(Op))) {
10254 if (GV->isConstant() && GV->hasInitializer()) {
10255 if (GV->getInitializer()->isNullValue())
10256 return ReplaceInstUsesWith(LI, Constant::getNullValue(LI.getType()));
10257 else if (isa<UndefValue>(GV->getInitializer()))
10258 return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType()));
10259 }
10260 }
Chris Lattnere228ee52004-04-08 20:39:49 +000010261
Chris Lattnera9d84e32005-05-01 04:24:53 +000010262 if (Op->hasOneUse()) {
Chris Lattnerf62ea8e2004-09-19 18:43:46 +000010263 // Change select and PHI nodes to select values instead of addresses: this
10264 // helps alias analysis out a lot, allows many others simplifications, and
10265 // exposes redundancy in the code.
10266 //
10267 // Note that we cannot do the transformation unless we know that the
10268 // introduced loads cannot trap! Something like this is valid as long as
10269 // the condition is always false: load (select bool %C, int* null, int* %G),
10270 // but it would not be valid if we transformed it to load from null
10271 // unconditionally.
10272 //
10273 if (SelectInst *SI = dyn_cast<SelectInst>(Op)) {
10274 // load (select (Cond, &V1, &V2)) --> select(Cond, load &V1, load &V2).
Chris Lattnere6f13092004-09-19 19:18:10 +000010275 if (isSafeToLoadUnconditionally(SI->getOperand(1), SI) &&
10276 isSafeToLoadUnconditionally(SI->getOperand(2), SI)) {
Chris Lattnerf62ea8e2004-09-19 18:43:46 +000010277 Value *V1 = InsertNewInstBefore(new LoadInst(SI->getOperand(1),
Chris Lattner42618552004-09-20 10:15:10 +000010278 SI->getOperand(1)->getName()+".val"), LI);
Chris Lattnerf62ea8e2004-09-19 18:43:46 +000010279 Value *V2 = InsertNewInstBefore(new LoadInst(SI->getOperand(2),
Chris Lattner42618552004-09-20 10:15:10 +000010280 SI->getOperand(2)->getName()+".val"), LI);
Gabor Greife9ecc682008-04-06 20:25:17 +000010281 return SelectInst::Create(SI->getCondition(), V1, V2);
Chris Lattnerf62ea8e2004-09-19 18:43:46 +000010282 }
10283
Chris Lattnerbdcf41a2004-09-23 15:46:00 +000010284 // load (select (cond, null, P)) -> load P
10285 if (Constant *C = dyn_cast<Constant>(SI->getOperand(1)))
10286 if (C->isNullValue()) {
10287 LI.setOperand(0, SI->getOperand(2));
10288 return &LI;
10289 }
10290
10291 // load (select (cond, P, null)) -> load P
10292 if (Constant *C = dyn_cast<Constant>(SI->getOperand(2)))
10293 if (C->isNullValue()) {
10294 LI.setOperand(0, SI->getOperand(1));
10295 return &LI;
10296 }
Chris Lattnerf62ea8e2004-09-19 18:43:46 +000010297 }
10298 }
Chris Lattner0f1d8a32003-06-26 05:06:25 +000010299 return 0;
10300}
10301
Reid Spencere928a152007-01-19 21:20:31 +000010302/// InstCombineStoreToCast - Fold store V, (cast P) -> store (cast V), P
Chris Lattner72684fe2005-01-31 05:51:45 +000010303/// when possible.
10304static Instruction *InstCombineStoreToCast(InstCombiner &IC, StoreInst &SI) {
10305 User *CI = cast<User>(SI.getOperand(1));
10306 Value *CastOp = CI->getOperand(0);
10307
10308 const Type *DestPTy = cast<PointerType>(CI->getType())->getElementType();
10309 if (const PointerType *SrcTy = dyn_cast<PointerType>(CastOp->getType())) {
10310 const Type *SrcPTy = SrcTy->getElementType();
10311
Reid Spencer31a4ef42007-01-22 05:51:25 +000010312 if (DestPTy->isInteger() || isa<PointerType>(DestPTy)) {
Chris Lattner72684fe2005-01-31 05:51:45 +000010313 // If the source is an array, the code below will not succeed. Check to
10314 // see if a trivial 'gep P, 0, 0' will help matters. Only do this for
10315 // constants.
10316 if (const ArrayType *ASrcTy = dyn_cast<ArrayType>(SrcPTy))
10317 if (Constant *CSrc = dyn_cast<Constant>(CastOp))
10318 if (ASrcTy->getNumElements() != 0) {
Chris Lattnerf96f4a82007-01-31 04:40:53 +000010319 Value* Idxs[2];
10320 Idxs[0] = Idxs[1] = Constant::getNullValue(Type::Int32Ty);
10321 CastOp = ConstantExpr::getGetElementPtr(CSrc, Idxs, 2);
Chris Lattner72684fe2005-01-31 05:51:45 +000010322 SrcTy = cast<PointerType>(CastOp->getType());
10323 SrcPTy = SrcTy->getElementType();
10324 }
10325
Reid Spencer9a4bed02007-01-20 23:35:48 +000010326 if ((SrcPTy->isInteger() || isa<PointerType>(SrcPTy)) &&
10327 IC.getTargetData().getTypeSizeInBits(SrcPTy) ==
10328 IC.getTargetData().getTypeSizeInBits(DestPTy)) {
Chris Lattner72684fe2005-01-31 05:51:45 +000010329
10330 // Okay, we are casting from one integer or pointer type to another of
Reid Spencerc050af92007-01-18 18:54:33 +000010331 // the same size. Instead of casting the pointer before
10332 // the store, cast the value to be stored.
Chris Lattner72684fe2005-01-31 05:51:45 +000010333 Value *NewCast;
Reid Spencerbb65ebf2006-12-12 23:36:14 +000010334 Value *SIOp0 = SI.getOperand(0);
Reid Spencerc050af92007-01-18 18:54:33 +000010335 Instruction::CastOps opcode = Instruction::BitCast;
10336 const Type* CastSrcTy = SIOp0->getType();
10337 const Type* CastDstTy = SrcPTy;
10338 if (isa<PointerType>(CastDstTy)) {
10339 if (CastSrcTy->isInteger())
Reid Spencerbb65ebf2006-12-12 23:36:14 +000010340 opcode = Instruction::IntToPtr;
Reid Spencer9a4bed02007-01-20 23:35:48 +000010341 } else if (isa<IntegerType>(CastDstTy)) {
Reid Spencer74a528b2006-12-13 18:21:21 +000010342 if (isa<PointerType>(SIOp0->getType()))
Reid Spencerbb65ebf2006-12-12 23:36:14 +000010343 opcode = Instruction::PtrToInt;
10344 }
10345 if (Constant *C = dyn_cast<Constant>(SIOp0))
Reid Spencerc050af92007-01-18 18:54:33 +000010346 NewCast = ConstantExpr::getCast(opcode, C, CastDstTy);
Chris Lattner72684fe2005-01-31 05:51:45 +000010347 else
Reid Spencer6c38f0b2006-11-27 01:05:10 +000010348 NewCast = IC.InsertNewInstBefore(
Reid Spencerc050af92007-01-18 18:54:33 +000010349 CastInst::create(opcode, SIOp0, CastDstTy, SIOp0->getName()+".c"),
10350 SI);
Chris Lattner72684fe2005-01-31 05:51:45 +000010351 return new StoreInst(NewCast, CastOp);
10352 }
10353 }
10354 }
10355 return 0;
10356}
10357
Chris Lattner31f486c2005-01-31 05:36:43 +000010358Instruction *InstCombiner::visitStoreInst(StoreInst &SI) {
10359 Value *Val = SI.getOperand(0);
10360 Value *Ptr = SI.getOperand(1);
10361
10362 if (isa<UndefValue>(Ptr)) { // store X, undef -> noop (even if volatile)
Chris Lattner5997cf92006-02-08 03:25:32 +000010363 EraseInstFromFunction(SI);
Chris Lattner31f486c2005-01-31 05:36:43 +000010364 ++NumCombined;
10365 return 0;
10366 }
Chris Lattnera4beeef2007-01-15 06:51:56 +000010367
10368 // If the RHS is an alloca with a single use, zapify the store, making the
10369 // alloca dead.
Chris Lattnere331a652008-04-29 04:58:38 +000010370 if (Ptr->hasOneUse() && !SI.isVolatile()) {
Chris Lattnera4beeef2007-01-15 06:51:56 +000010371 if (isa<AllocaInst>(Ptr)) {
10372 EraseInstFromFunction(SI);
10373 ++NumCombined;
10374 return 0;
10375 }
10376
10377 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr))
10378 if (isa<AllocaInst>(GEP->getOperand(0)) &&
10379 GEP->getOperand(0)->hasOneUse()) {
10380 EraseInstFromFunction(SI);
10381 ++NumCombined;
10382 return 0;
10383 }
10384 }
Chris Lattner31f486c2005-01-31 05:36:43 +000010385
Dan Gohmane31a61e2007-07-20 16:34:21 +000010386 // Attempt to improve the alignment.
Dan Gohman99b7b3f2008-04-10 18:43:06 +000010387 unsigned KnownAlign = GetOrEnforceKnownAlignment(Ptr);
10388 if (KnownAlign >
10389 (SI.getAlignment() == 0 ? TD->getABITypeAlignment(Val->getType()) :
10390 SI.getAlignment()))
Dan Gohmane31a61e2007-07-20 16:34:21 +000010391 SI.setAlignment(KnownAlign);
10392
Chris Lattner5997cf92006-02-08 03:25:32 +000010393 // Do really simple DSE, to catch cases where there are several consequtive
10394 // stores to the same location, separated by a few arithmetic operations. This
10395 // situation often occurs with bitfield accesses.
10396 BasicBlock::iterator BBI = &SI;
10397 for (unsigned ScanInsts = 6; BBI != SI.getParent()->begin() && ScanInsts;
10398 --ScanInsts) {
10399 --BBI;
10400
10401 if (StoreInst *PrevSI = dyn_cast<StoreInst>(BBI)) {
10402 // Prev store isn't volatile, and stores to the same location?
10403 if (!PrevSI->isVolatile() && PrevSI->getOperand(1) == SI.getOperand(1)) {
10404 ++NumDeadStore;
10405 ++BBI;
10406 EraseInstFromFunction(*PrevSI);
10407 continue;
10408 }
10409 break;
10410 }
10411
Chris Lattnerdab43b22006-05-26 19:19:20 +000010412 // If this is a load, we have to stop. However, if the loaded value is from
10413 // the pointer we're loading and is producing the pointer we're storing,
10414 // then *this* store is dead (X = load P; store X -> P).
10415 if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) {
Chris Lattner85a51e02007-09-07 05:33:03 +000010416 if (LI == Val && LI->getOperand(0) == Ptr && !SI.isVolatile()) {
Chris Lattnerdab43b22006-05-26 19:19:20 +000010417 EraseInstFromFunction(SI);
10418 ++NumCombined;
10419 return 0;
10420 }
10421 // Otherwise, this is a load from some other location. Stores before it
10422 // may not be dead.
10423 break;
10424 }
10425
Chris Lattner5997cf92006-02-08 03:25:32 +000010426 // Don't skip over loads or things that can modify memory.
Chris Lattnerdab43b22006-05-26 19:19:20 +000010427 if (BBI->mayWriteToMemory())
Chris Lattner5997cf92006-02-08 03:25:32 +000010428 break;
10429 }
10430
10431
10432 if (SI.isVolatile()) return 0; // Don't hack volatile stores.
Chris Lattner31f486c2005-01-31 05:36:43 +000010433
10434 // store X, null -> turns into 'unreachable' in SimplifyCFG
10435 if (isa<ConstantPointerNull>(Ptr)) {
10436 if (!isa<UndefValue>(Val)) {
10437 SI.setOperand(0, UndefValue::get(Val->getType()));
10438 if (Instruction *U = dyn_cast<Instruction>(Val))
Chris Lattnerb15e2b12007-03-02 21:28:56 +000010439 AddToWorkList(U); // Dropped a use.
Chris Lattner31f486c2005-01-31 05:36:43 +000010440 ++NumCombined;
10441 }
10442 return 0; // Do not modify these!
10443 }
10444
10445 // store undef, Ptr -> noop
10446 if (isa<UndefValue>(Val)) {
Chris Lattner5997cf92006-02-08 03:25:32 +000010447 EraseInstFromFunction(SI);
Chris Lattner31f486c2005-01-31 05:36:43 +000010448 ++NumCombined;
10449 return 0;
10450 }
10451
Chris Lattner72684fe2005-01-31 05:51:45 +000010452 // If the pointer destination is a cast, see if we can fold the cast into the
10453 // source instead.
Reid Spencerde46e482006-11-02 20:25:50 +000010454 if (isa<CastInst>(Ptr))
Chris Lattner72684fe2005-01-31 05:51:45 +000010455 if (Instruction *Res = InstCombineStoreToCast(*this, SI))
10456 return Res;
10457 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr))
Reid Spencer6c38f0b2006-11-27 01:05:10 +000010458 if (CE->isCast())
Chris Lattner72684fe2005-01-31 05:51:45 +000010459 if (Instruction *Res = InstCombineStoreToCast(*this, SI))
10460 return Res;
10461
Chris Lattner219175c2005-09-12 23:23:25 +000010462
10463 // If this store is the last instruction in the basic block, and if the block
10464 // ends with an unconditional branch, try to move it to the successor block.
Chris Lattner5997cf92006-02-08 03:25:32 +000010465 BBI = &SI; ++BBI;
Chris Lattner219175c2005-09-12 23:23:25 +000010466 if (BranchInst *BI = dyn_cast<BranchInst>(BBI))
Chris Lattner14a251b2007-04-15 00:07:55 +000010467 if (BI->isUnconditional())
10468 if (SimplifyStoreAtEndOfBlock(SI))
10469 return 0; // xform done!
Chris Lattner219175c2005-09-12 23:23:25 +000010470
Chris Lattner31f486c2005-01-31 05:36:43 +000010471 return 0;
10472}
10473
Chris Lattner14a251b2007-04-15 00:07:55 +000010474/// SimplifyStoreAtEndOfBlock - Turn things like:
10475/// if () { *P = v1; } else { *P = v2 }
10476/// into a phi node with a store in the successor.
10477///
Chris Lattner4a6e0cb2007-04-15 01:02:18 +000010478/// Simplify things like:
10479/// *P = v1; if () { *P = v2; }
10480/// into a phi node with a store in the successor.
10481///
Chris Lattner14a251b2007-04-15 00:07:55 +000010482bool InstCombiner::SimplifyStoreAtEndOfBlock(StoreInst &SI) {
10483 BasicBlock *StoreBB = SI.getParent();
10484
10485 // Check to see if the successor block has exactly two incoming edges. If
10486 // so, see if the other predecessor contains a store to the same location.
10487 // if so, insert a PHI node (if needed) and move the stores down.
Chris Lattner4a6e0cb2007-04-15 01:02:18 +000010488 BasicBlock *DestBB = StoreBB->getTerminator()->getSuccessor(0);
Chris Lattner14a251b2007-04-15 00:07:55 +000010489
10490 // Determine whether Dest has exactly two predecessors and, if so, compute
10491 // the other predecessor.
Chris Lattner4a6e0cb2007-04-15 01:02:18 +000010492 pred_iterator PI = pred_begin(DestBB);
10493 BasicBlock *OtherBB = 0;
Chris Lattner14a251b2007-04-15 00:07:55 +000010494 if (*PI != StoreBB)
Chris Lattner4a6e0cb2007-04-15 01:02:18 +000010495 OtherBB = *PI;
Chris Lattner14a251b2007-04-15 00:07:55 +000010496 ++PI;
Chris Lattner4a6e0cb2007-04-15 01:02:18 +000010497 if (PI == pred_end(DestBB))
Chris Lattner14a251b2007-04-15 00:07:55 +000010498 return false;
10499
10500 if (*PI != StoreBB) {
Chris Lattner4a6e0cb2007-04-15 01:02:18 +000010501 if (OtherBB)
Chris Lattner14a251b2007-04-15 00:07:55 +000010502 return false;
Chris Lattner4a6e0cb2007-04-15 01:02:18 +000010503 OtherBB = *PI;
Chris Lattner14a251b2007-04-15 00:07:55 +000010504 }
Chris Lattner4a6e0cb2007-04-15 01:02:18 +000010505 if (++PI != pred_end(DestBB))
Chris Lattner14a251b2007-04-15 00:07:55 +000010506 return false;
10507
10508
Chris Lattner4a6e0cb2007-04-15 01:02:18 +000010509 // Verify that the other block ends in a branch and is not otherwise empty.
10510 BasicBlock::iterator BBI = OtherBB->getTerminator();
Chris Lattner14a251b2007-04-15 00:07:55 +000010511 BranchInst *OtherBr = dyn_cast<BranchInst>(BBI);
Chris Lattner4a6e0cb2007-04-15 01:02:18 +000010512 if (!OtherBr || BBI == OtherBB->begin())
Chris Lattner14a251b2007-04-15 00:07:55 +000010513 return false;
10514
Chris Lattner4a6e0cb2007-04-15 01:02:18 +000010515 // If the other block ends in an unconditional branch, check for the 'if then
10516 // else' case. there is an instruction before the branch.
10517 StoreInst *OtherStore = 0;
10518 if (OtherBr->isUnconditional()) {
10519 // If this isn't a store, or isn't a store to the same location, bail out.
10520 --BBI;
10521 OtherStore = dyn_cast<StoreInst>(BBI);
10522 if (!OtherStore || OtherStore->getOperand(1) != SI.getOperand(1))
10523 return false;
10524 } else {
Chris Lattner361e9812007-05-05 22:32:24 +000010525 // Otherwise, the other block ended with a conditional branch. If one of the
Chris Lattner4a6e0cb2007-04-15 01:02:18 +000010526 // destinations is StoreBB, then we have the if/then case.
10527 if (OtherBr->getSuccessor(0) != StoreBB &&
10528 OtherBr->getSuccessor(1) != StoreBB)
10529 return false;
10530
10531 // Okay, we know that OtherBr now goes to Dest and StoreBB, so this is an
Chris Lattner361e9812007-05-05 22:32:24 +000010532 // if/then triangle. See if there is a store to the same ptr as SI that
10533 // lives in OtherBB.
Chris Lattner4a6e0cb2007-04-15 01:02:18 +000010534 for (;; --BBI) {
10535 // Check to see if we find the matching store.
10536 if ((OtherStore = dyn_cast<StoreInst>(BBI))) {
10537 if (OtherStore->getOperand(1) != SI.getOperand(1))
10538 return false;
10539 break;
10540 }
Chris Lattner361e9812007-05-05 22:32:24 +000010541 // If we find something that may be using the stored value, or if we run
10542 // out of instructions, we can't do the xform.
Chris Lattner4a6e0cb2007-04-15 01:02:18 +000010543 if (isa<LoadInst>(BBI) || BBI->mayWriteToMemory() ||
10544 BBI == OtherBB->begin())
10545 return false;
10546 }
10547
10548 // In order to eliminate the store in OtherBr, we have to
10549 // make sure nothing reads the stored value in StoreBB.
10550 for (BasicBlock::iterator I = StoreBB->begin(); &*I != &SI; ++I) {
10551 // FIXME: This should really be AA driven.
10552 if (isa<LoadInst>(I) || I->mayWriteToMemory())
10553 return false;
10554 }
10555 }
Chris Lattner14a251b2007-04-15 00:07:55 +000010556
Chris Lattner4a6e0cb2007-04-15 01:02:18 +000010557 // Insert a PHI node now if we need it.
Chris Lattner14a251b2007-04-15 00:07:55 +000010558 Value *MergedVal = OtherStore->getOperand(0);
10559 if (MergedVal != SI.getOperand(0)) {
Gabor Greife9ecc682008-04-06 20:25:17 +000010560 PHINode *PN = PHINode::Create(MergedVal->getType(), "storemerge");
Chris Lattner14a251b2007-04-15 00:07:55 +000010561 PN->reserveOperandSpace(2);
10562 PN->addIncoming(SI.getOperand(0), SI.getParent());
Chris Lattner4a6e0cb2007-04-15 01:02:18 +000010563 PN->addIncoming(OtherStore->getOperand(0), OtherBB);
10564 MergedVal = InsertNewInstBefore(PN, DestBB->front());
Chris Lattner14a251b2007-04-15 00:07:55 +000010565 }
10566
10567 // Advance to a place where it is safe to insert the new store and
10568 // insert it.
Chris Lattner4a6e0cb2007-04-15 01:02:18 +000010569 BBI = DestBB->begin();
Chris Lattner14a251b2007-04-15 00:07:55 +000010570 while (isa<PHINode>(BBI)) ++BBI;
10571 InsertNewInstBefore(new StoreInst(MergedVal, SI.getOperand(1),
10572 OtherStore->isVolatile()), *BBI);
10573
10574 // Nuke the old stores.
10575 EraseInstFromFunction(SI);
10576 EraseInstFromFunction(*OtherStore);
10577 ++NumCombined;
10578 return true;
10579}
10580
Chris Lattner31f486c2005-01-31 05:36:43 +000010581
Chris Lattner9eef8a72003-06-04 04:46:00 +000010582Instruction *InstCombiner::visitBranchInst(BranchInst &BI) {
10583 // Change br (not X), label True, label False to: br X, label False, True
Reid Spencer4fdd96c2005-06-18 17:37:34 +000010584 Value *X = 0;
Chris Lattnerd4252a72004-07-30 07:50:03 +000010585 BasicBlock *TrueDest;
10586 BasicBlock *FalseDest;
10587 if (match(&BI, m_Br(m_Not(m_Value(X)), TrueDest, FalseDest)) &&
10588 !isa<Constant>(X)) {
10589 // Swap Destinations and condition...
10590 BI.setCondition(X);
10591 BI.setSuccessor(0, FalseDest);
10592 BI.setSuccessor(1, TrueDest);
10593 return &BI;
10594 }
10595
Reid Spencer266e42b2006-12-23 06:05:41 +000010596 // Cannonicalize fcmp_one -> fcmp_oeq
10597 FCmpInst::Predicate FPred; Value *Y;
10598 if (match(&BI, m_Br(m_FCmp(FPred, m_Value(X), m_Value(Y)),
10599 TrueDest, FalseDest)))
10600 if ((FPred == FCmpInst::FCMP_ONE || FPred == FCmpInst::FCMP_OLE ||
10601 FPred == FCmpInst::FCMP_OGE) && BI.getCondition()->hasOneUse()) {
10602 FCmpInst *I = cast<FCmpInst>(BI.getCondition());
Reid Spencer266e42b2006-12-23 06:05:41 +000010603 FCmpInst::Predicate NewPred = FCmpInst::getInversePredicate(FPred);
Chris Lattner6e0123b2007-02-11 01:23:03 +000010604 Instruction *NewSCC = new FCmpInst(NewPred, X, Y, "", I);
10605 NewSCC->takeName(I);
Reid Spencer266e42b2006-12-23 06:05:41 +000010606 // Swap Destinations and condition...
10607 BI.setCondition(NewSCC);
10608 BI.setSuccessor(0, FalseDest);
10609 BI.setSuccessor(1, TrueDest);
Chris Lattnerb15e2b12007-03-02 21:28:56 +000010610 RemoveFromWorkList(I);
Chris Lattner6e0123b2007-02-11 01:23:03 +000010611 I->eraseFromParent();
Chris Lattnerb15e2b12007-03-02 21:28:56 +000010612 AddToWorkList(NewSCC);
Reid Spencer266e42b2006-12-23 06:05:41 +000010613 return &BI;
10614 }
10615
10616 // Cannonicalize icmp_ne -> icmp_eq
10617 ICmpInst::Predicate IPred;
10618 if (match(&BI, m_Br(m_ICmp(IPred, m_Value(X), m_Value(Y)),
10619 TrueDest, FalseDest)))
10620 if ((IPred == ICmpInst::ICMP_NE || IPred == ICmpInst::ICMP_ULE ||
10621 IPred == ICmpInst::ICMP_SLE || IPred == ICmpInst::ICMP_UGE ||
10622 IPred == ICmpInst::ICMP_SGE) && BI.getCondition()->hasOneUse()) {
10623 ICmpInst *I = cast<ICmpInst>(BI.getCondition());
Reid Spencer266e42b2006-12-23 06:05:41 +000010624 ICmpInst::Predicate NewPred = ICmpInst::getInversePredicate(IPred);
Chris Lattner6e0123b2007-02-11 01:23:03 +000010625 Instruction *NewSCC = new ICmpInst(NewPred, X, Y, "", I);
10626 NewSCC->takeName(I);
Chris Lattnere967b342003-06-04 05:10:11 +000010627 // Swap Destinations and condition...
Chris Lattnerd4252a72004-07-30 07:50:03 +000010628 BI.setCondition(NewSCC);
Chris Lattnere967b342003-06-04 05:10:11 +000010629 BI.setSuccessor(0, FalseDest);
10630 BI.setSuccessor(1, TrueDest);
Chris Lattnerb15e2b12007-03-02 21:28:56 +000010631 RemoveFromWorkList(I);
Chris Lattner6e0123b2007-02-11 01:23:03 +000010632 I->eraseFromParent();;
Chris Lattnerb15e2b12007-03-02 21:28:56 +000010633 AddToWorkList(NewSCC);
Chris Lattnere967b342003-06-04 05:10:11 +000010634 return &BI;
10635 }
Misha Brukmanb1c93172005-04-21 23:48:37 +000010636
Chris Lattner9eef8a72003-06-04 04:46:00 +000010637 return 0;
10638}
Chris Lattner1085bdf2002-11-04 16:18:53 +000010639
Chris Lattner4c9c20a2004-07-03 00:26:11 +000010640Instruction *InstCombiner::visitSwitchInst(SwitchInst &SI) {
10641 Value *Cond = SI.getCondition();
10642 if (Instruction *I = dyn_cast<Instruction>(Cond)) {
10643 if (I->getOpcode() == Instruction::Add)
10644 if (ConstantInt *AddRHS = dyn_cast<ConstantInt>(I->getOperand(1))) {
10645 // change 'switch (X+4) case 1:' into 'switch (X) case -3'
10646 for (unsigned i = 2, e = SI.getNumOperands(); i != e; i += 2)
Chris Lattner81a7a232004-10-16 18:11:37 +000010647 SI.setOperand(i,ConstantExpr::getSub(cast<Constant>(SI.getOperand(i)),
Chris Lattner4c9c20a2004-07-03 00:26:11 +000010648 AddRHS));
10649 SI.setOperand(0, I->getOperand(0));
Chris Lattnerb15e2b12007-03-02 21:28:56 +000010650 AddToWorkList(I);
Chris Lattner4c9c20a2004-07-03 00:26:11 +000010651 return &SI;
10652 }
10653 }
10654 return 0;
10655}
10656
Chris Lattner6bc98652006-03-05 00:22:33 +000010657/// CheapToScalarize - Return true if the value is cheaper to scalarize than it
10658/// is to leave as a vector operation.
10659static bool CheapToScalarize(Value *V, bool isConstant) {
10660 if (isa<ConstantAggregateZero>(V))
10661 return true;
Reid Spencerd84d35b2007-02-15 02:26:10 +000010662 if (ConstantVector *C = dyn_cast<ConstantVector>(V)) {
Chris Lattner6bc98652006-03-05 00:22:33 +000010663 if (isConstant) return true;
10664 // If all elts are the same, we can extract.
10665 Constant *Op0 = C->getOperand(0);
10666 for (unsigned i = 1; i < C->getNumOperands(); ++i)
10667 if (C->getOperand(i) != Op0)
10668 return false;
10669 return true;
10670 }
10671 Instruction *I = dyn_cast<Instruction>(V);
10672 if (!I) return false;
10673
10674 // Insert element gets simplified to the inserted element or is deleted if
10675 // this is constant idx extract element and its a constant idx insertelt.
10676 if (I->getOpcode() == Instruction::InsertElement && isConstant &&
10677 isa<ConstantInt>(I->getOperand(2)))
10678 return true;
10679 if (I->getOpcode() == Instruction::Load && I->hasOneUse())
10680 return true;
10681 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I))
10682 if (BO->hasOneUse() &&
10683 (CheapToScalarize(BO->getOperand(0), isConstant) ||
10684 CheapToScalarize(BO->getOperand(1), isConstant)))
10685 return true;
Reid Spencer266e42b2006-12-23 06:05:41 +000010686 if (CmpInst *CI = dyn_cast<CmpInst>(I))
10687 if (CI->hasOneUse() &&
10688 (CheapToScalarize(CI->getOperand(0), isConstant) ||
10689 CheapToScalarize(CI->getOperand(1), isConstant)))
10690 return true;
Chris Lattner6bc98652006-03-05 00:22:33 +000010691
10692 return false;
10693}
10694
Chris Lattner945e4372007-02-14 05:52:17 +000010695/// Read and decode a shufflevector mask.
10696///
10697/// It turns undef elements into values that are larger than the number of
10698/// elements in the input.
Chris Lattner12249be2006-05-25 23:48:38 +000010699static std::vector<unsigned> getShuffleMask(const ShuffleVectorInst *SVI) {
10700 unsigned NElts = SVI->getType()->getNumElements();
10701 if (isa<ConstantAggregateZero>(SVI->getOperand(2)))
10702 return std::vector<unsigned>(NElts, 0);
10703 if (isa<UndefValue>(SVI->getOperand(2)))
10704 return std::vector<unsigned>(NElts, 2*NElts);
10705
10706 std::vector<unsigned> Result;
Reid Spencerd84d35b2007-02-15 02:26:10 +000010707 const ConstantVector *CP = cast<ConstantVector>(SVI->getOperand(2));
Chris Lattner12249be2006-05-25 23:48:38 +000010708 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
10709 if (isa<UndefValue>(CP->getOperand(i)))
10710 Result.push_back(NElts*2); // undef -> 8
10711 else
Reid Spencere0fc4df2006-10-20 07:07:24 +000010712 Result.push_back(cast<ConstantInt>(CP->getOperand(i))->getZExtValue());
Chris Lattner12249be2006-05-25 23:48:38 +000010713 return Result;
10714}
10715
Chris Lattner8d1d8d32006-03-31 23:01:56 +000010716/// FindScalarElement - Given a vector and an element number, see if the scalar
10717/// value is already around as a register, for example if it were inserted then
10718/// extracted from the vector.
10719static Value *FindScalarElement(Value *V, unsigned EltNo) {
Reid Spencerd84d35b2007-02-15 02:26:10 +000010720 assert(isa<VectorType>(V->getType()) && "Not looking at a vector?");
10721 const VectorType *PTy = cast<VectorType>(V->getType());
Chris Lattner2d37f922006-04-10 23:06:36 +000010722 unsigned Width = PTy->getNumElements();
10723 if (EltNo >= Width) // Out of range access.
Chris Lattner8d1d8d32006-03-31 23:01:56 +000010724 return UndefValue::get(PTy->getElementType());
10725
10726 if (isa<UndefValue>(V))
10727 return UndefValue::get(PTy->getElementType());
10728 else if (isa<ConstantAggregateZero>(V))
10729 return Constant::getNullValue(PTy->getElementType());
Reid Spencerd84d35b2007-02-15 02:26:10 +000010730 else if (ConstantVector *CP = dyn_cast<ConstantVector>(V))
Chris Lattner8d1d8d32006-03-31 23:01:56 +000010731 return CP->getOperand(EltNo);
10732 else if (InsertElementInst *III = dyn_cast<InsertElementInst>(V)) {
10733 // If this is an insert to a variable element, we don't know what it is.
Reid Spencere0fc4df2006-10-20 07:07:24 +000010734 if (!isa<ConstantInt>(III->getOperand(2)))
10735 return 0;
10736 unsigned IIElt = cast<ConstantInt>(III->getOperand(2))->getZExtValue();
Chris Lattner8d1d8d32006-03-31 23:01:56 +000010737
10738 // If this is an insert to the element we are looking for, return the
10739 // inserted value.
Reid Spencere0fc4df2006-10-20 07:07:24 +000010740 if (EltNo == IIElt)
10741 return III->getOperand(1);
Chris Lattner8d1d8d32006-03-31 23:01:56 +000010742
10743 // Otherwise, the insertelement doesn't modify the value, recurse on its
10744 // vector input.
10745 return FindScalarElement(III->getOperand(0), EltNo);
Chris Lattner2d37f922006-04-10 23:06:36 +000010746 } else if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(V)) {
Chris Lattner12249be2006-05-25 23:48:38 +000010747 unsigned InEl = getShuffleMask(SVI)[EltNo];
10748 if (InEl < Width)
10749 return FindScalarElement(SVI->getOperand(0), InEl);
10750 else if (InEl < Width*2)
10751 return FindScalarElement(SVI->getOperand(1), InEl - Width);
10752 else
10753 return UndefValue::get(PTy->getElementType());
Chris Lattner8d1d8d32006-03-31 23:01:56 +000010754 }
10755
10756 // Otherwise, we don't know.
10757 return 0;
10758}
10759
Robert Bocchinoa8352962006-01-13 22:48:06 +000010760Instruction *InstCombiner::visitExtractElementInst(ExtractElementInst &EI) {
Chris Lattner8d1d8d32006-03-31 23:01:56 +000010761
Dan Gohman06c60b62007-07-16 14:29:03 +000010762 // If vector val is undef, replace extract with scalar undef.
Chris Lattner92346c32006-03-31 18:25:14 +000010763 if (isa<UndefValue>(EI.getOperand(0)))
10764 return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType()));
10765
Dan Gohman06c60b62007-07-16 14:29:03 +000010766 // If vector val is constant 0, replace extract with scalar 0.
Chris Lattner92346c32006-03-31 18:25:14 +000010767 if (isa<ConstantAggregateZero>(EI.getOperand(0)))
10768 return ReplaceInstUsesWith(EI, Constant::getNullValue(EI.getType()));
10769
Reid Spencerd84d35b2007-02-15 02:26:10 +000010770 if (ConstantVector *C = dyn_cast<ConstantVector>(EI.getOperand(0))) {
Dan Gohman06c60b62007-07-16 14:29:03 +000010771 // If vector val is constant with uniform operands, replace EI
Robert Bocchinoa8352962006-01-13 22:48:06 +000010772 // with that operand
Chris Lattner6bc98652006-03-05 00:22:33 +000010773 Constant *op0 = C->getOperand(0);
Robert Bocchinoa8352962006-01-13 22:48:06 +000010774 for (unsigned i = 1; i < C->getNumOperands(); ++i)
Chris Lattner6bc98652006-03-05 00:22:33 +000010775 if (C->getOperand(i) != op0) {
10776 op0 = 0;
10777 break;
10778 }
10779 if (op0)
10780 return ReplaceInstUsesWith(EI, op0);
Robert Bocchinoa8352962006-01-13 22:48:06 +000010781 }
Chris Lattner6bc98652006-03-05 00:22:33 +000010782
Chris Lattner8d1d8d32006-03-31 23:01:56 +000010783 // If extracting a specified index from the vector, see if we can recursively
10784 // find a previously computed scalar that was inserted into the vector.
Reid Spencere0fc4df2006-10-20 07:07:24 +000010785 if (ConstantInt *IdxC = dyn_cast<ConstantInt>(EI.getOperand(1))) {
Chris Lattnera87c9f62007-04-09 01:37:55 +000010786 unsigned IndexVal = IdxC->getZExtValue();
10787 unsigned VectorWidth =
10788 cast<VectorType>(EI.getOperand(0)->getType())->getNumElements();
10789
10790 // If this is extracting an invalid index, turn this into undef, to avoid
10791 // crashing the code below.
10792 if (IndexVal >= VectorWidth)
10793 return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType()));
10794
Chris Lattner2deeaea2006-10-05 06:55:50 +000010795 // This instruction only demands the single element from the input vector.
10796 // If the input vector has a single use, simplify it based on this use
10797 // property.
Chris Lattnera87c9f62007-04-09 01:37:55 +000010798 if (EI.getOperand(0)->hasOneUse() && VectorWidth != 1) {
Chris Lattner2deeaea2006-10-05 06:55:50 +000010799 uint64_t UndefElts;
10800 if (Value *V = SimplifyDemandedVectorElts(EI.getOperand(0),
Reid Spencere0fc4df2006-10-20 07:07:24 +000010801 1 << IndexVal,
Chris Lattner2deeaea2006-10-05 06:55:50 +000010802 UndefElts)) {
10803 EI.setOperand(0, V);
10804 return &EI;
10805 }
10806 }
10807
Reid Spencere0fc4df2006-10-20 07:07:24 +000010808 if (Value *Elt = FindScalarElement(EI.getOperand(0), IndexVal))
Chris Lattner8d1d8d32006-03-31 23:01:56 +000010809 return ReplaceInstUsesWith(EI, Elt);
Chris Lattner7bfdd0a2007-04-14 23:02:14 +000010810
10811 // If the this extractelement is directly using a bitcast from a vector of
10812 // the same number of elements, see if we can find the source element from
10813 // it. In this case, we will end up needing to bitcast the scalars.
10814 if (BitCastInst *BCI = dyn_cast<BitCastInst>(EI.getOperand(0))) {
10815 if (const VectorType *VT =
10816 dyn_cast<VectorType>(BCI->getOperand(0)->getType()))
10817 if (VT->getNumElements() == VectorWidth)
10818 if (Value *Elt = FindScalarElement(BCI->getOperand(0), IndexVal))
10819 return new BitCastInst(Elt, EI.getType());
10820 }
Chris Lattner2d37f922006-04-10 23:06:36 +000010821 }
Chris Lattner8d1d8d32006-03-31 23:01:56 +000010822
Chris Lattner83f65782006-05-25 22:53:38 +000010823 if (Instruction *I = dyn_cast<Instruction>(EI.getOperand(0))) {
Robert Bocchinoa8352962006-01-13 22:48:06 +000010824 if (I->hasOneUse()) {
10825 // Push extractelement into predecessor operation if legal and
10826 // profitable to do so
10827 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I)) {
Chris Lattner6bc98652006-03-05 00:22:33 +000010828 bool isConstantElt = isa<ConstantInt>(EI.getOperand(1));
10829 if (CheapToScalarize(BO, isConstantElt)) {
10830 ExtractElementInst *newEI0 =
10831 new ExtractElementInst(BO->getOperand(0), EI.getOperand(1),
10832 EI.getName()+".lhs");
10833 ExtractElementInst *newEI1 =
10834 new ExtractElementInst(BO->getOperand(1), EI.getOperand(1),
10835 EI.getName()+".rhs");
10836 InsertNewInstBefore(newEI0, EI);
10837 InsertNewInstBefore(newEI1, EI);
10838 return BinaryOperator::create(BO->getOpcode(), newEI0, newEI1);
10839 }
Reid Spencerde46e482006-11-02 20:25:50 +000010840 } else if (isa<LoadInst>(I)) {
Christopher Lambedf07882007-12-17 01:12:55 +000010841 unsigned AS =
10842 cast<PointerType>(I->getOperand(0)->getType())->getAddressSpace();
Chris Lattner5a866122008-01-13 22:23:22 +000010843 Value *Ptr = InsertBitCastBefore(I->getOperand(0),
10844 PointerType::get(EI.getType(), AS),EI);
Robert Bocchinoa8352962006-01-13 22:48:06 +000010845 GetElementPtrInst *GEP =
Gabor Greife9ecc682008-04-06 20:25:17 +000010846 GetElementPtrInst::Create(Ptr, EI.getOperand(1), I->getName() + ".gep");
Robert Bocchinoa8352962006-01-13 22:48:06 +000010847 InsertNewInstBefore(GEP, EI);
10848 return new LoadInst(GEP);
Chris Lattner83f65782006-05-25 22:53:38 +000010849 }
10850 }
10851 if (InsertElementInst *IE = dyn_cast<InsertElementInst>(I)) {
10852 // Extracting the inserted element?
10853 if (IE->getOperand(2) == EI.getOperand(1))
10854 return ReplaceInstUsesWith(EI, IE->getOperand(1));
10855 // If the inserted and extracted elements are constants, they must not
10856 // be the same value, extract from the pre-inserted value instead.
10857 if (isa<Constant>(IE->getOperand(2)) &&
10858 isa<Constant>(EI.getOperand(1))) {
10859 AddUsesToWorkList(EI);
10860 EI.setOperand(0, IE->getOperand(0));
10861 return &EI;
10862 }
10863 } else if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(I)) {
10864 // If this is extracting an element from a shufflevector, figure out where
10865 // it came from and extract from the appropriate input element instead.
Reid Spencere0fc4df2006-10-20 07:07:24 +000010866 if (ConstantInt *Elt = dyn_cast<ConstantInt>(EI.getOperand(1))) {
10867 unsigned SrcIdx = getShuffleMask(SVI)[Elt->getZExtValue()];
Chris Lattner12249be2006-05-25 23:48:38 +000010868 Value *Src;
10869 if (SrcIdx < SVI->getType()->getNumElements())
10870 Src = SVI->getOperand(0);
10871 else if (SrcIdx < SVI->getType()->getNumElements()*2) {
10872 SrcIdx -= SVI->getType()->getNumElements();
10873 Src = SVI->getOperand(1);
10874 } else {
10875 return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType()));
Chris Lattner612fa8e2006-03-30 22:02:40 +000010876 }
Chris Lattner2deeaea2006-10-05 06:55:50 +000010877 return new ExtractElementInst(Src, SrcIdx);
Robert Bocchinoa8352962006-01-13 22:48:06 +000010878 }
10879 }
Chris Lattner83f65782006-05-25 22:53:38 +000010880 }
Robert Bocchinoa8352962006-01-13 22:48:06 +000010881 return 0;
10882}
10883
Chris Lattner90951862006-04-16 00:51:47 +000010884/// CollectSingleShuffleElements - If V is a shuffle of values that ONLY returns
10885/// elements from either LHS or RHS, return the shuffle mask and true.
10886/// Otherwise, return false.
10887static bool CollectSingleShuffleElements(Value *V, Value *LHS, Value *RHS,
10888 std::vector<Constant*> &Mask) {
10889 assert(V->getType() == LHS->getType() && V->getType() == RHS->getType() &&
10890 "Invalid CollectSingleShuffleElements");
Reid Spencerd84d35b2007-02-15 02:26:10 +000010891 unsigned NumElts = cast<VectorType>(V->getType())->getNumElements();
Chris Lattner90951862006-04-16 00:51:47 +000010892
10893 if (isa<UndefValue>(V)) {
Reid Spencerc635f472006-12-31 05:48:39 +000010894 Mask.assign(NumElts, UndefValue::get(Type::Int32Ty));
Chris Lattner90951862006-04-16 00:51:47 +000010895 return true;
10896 } else if (V == LHS) {
10897 for (unsigned i = 0; i != NumElts; ++i)
Reid Spencerc635f472006-12-31 05:48:39 +000010898 Mask.push_back(ConstantInt::get(Type::Int32Ty, i));
Chris Lattner90951862006-04-16 00:51:47 +000010899 return true;
10900 } else if (V == RHS) {
10901 for (unsigned i = 0; i != NumElts; ++i)
Reid Spencerc635f472006-12-31 05:48:39 +000010902 Mask.push_back(ConstantInt::get(Type::Int32Ty, i+NumElts));
Chris Lattner90951862006-04-16 00:51:47 +000010903 return true;
10904 } else if (InsertElementInst *IEI = dyn_cast<InsertElementInst>(V)) {
10905 // If this is an insert of an extract from some other vector, include it.
10906 Value *VecOp = IEI->getOperand(0);
10907 Value *ScalarOp = IEI->getOperand(1);
10908 Value *IdxOp = IEI->getOperand(2);
10909
Chris Lattnerb6cb64b2006-04-27 21:14:21 +000010910 if (!isa<ConstantInt>(IdxOp))
10911 return false;
Reid Spencere0fc4df2006-10-20 07:07:24 +000010912 unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue();
Chris Lattnerb6cb64b2006-04-27 21:14:21 +000010913
10914 if (isa<UndefValue>(ScalarOp)) { // inserting undef into vector.
10915 // Okay, we can handle this if the vector we are insertinting into is
10916 // transitively ok.
10917 if (CollectSingleShuffleElements(VecOp, LHS, RHS, Mask)) {
10918 // If so, update the mask to reflect the inserted undef.
Reid Spencerc635f472006-12-31 05:48:39 +000010919 Mask[InsertedIdx] = UndefValue::get(Type::Int32Ty);
Chris Lattnerb6cb64b2006-04-27 21:14:21 +000010920 return true;
10921 }
10922 } else if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)){
10923 if (isa<ConstantInt>(EI->getOperand(1)) &&
Chris Lattner90951862006-04-16 00:51:47 +000010924 EI->getOperand(0)->getType() == V->getType()) {
10925 unsigned ExtractedIdx =
Reid Spencere0fc4df2006-10-20 07:07:24 +000010926 cast<ConstantInt>(EI->getOperand(1))->getZExtValue();
Chris Lattner90951862006-04-16 00:51:47 +000010927
10928 // This must be extracting from either LHS or RHS.
10929 if (EI->getOperand(0) == LHS || EI->getOperand(0) == RHS) {
10930 // Okay, we can handle this if the vector we are insertinting into is
10931 // transitively ok.
10932 if (CollectSingleShuffleElements(VecOp, LHS, RHS, Mask)) {
10933 // If so, update the mask to reflect the inserted value.
10934 if (EI->getOperand(0) == LHS) {
10935 Mask[InsertedIdx & (NumElts-1)] =
Reid Spencerc635f472006-12-31 05:48:39 +000010936 ConstantInt::get(Type::Int32Ty, ExtractedIdx);
Chris Lattner90951862006-04-16 00:51:47 +000010937 } else {
10938 assert(EI->getOperand(0) == RHS);
10939 Mask[InsertedIdx & (NumElts-1)] =
Reid Spencerc635f472006-12-31 05:48:39 +000010940 ConstantInt::get(Type::Int32Ty, ExtractedIdx+NumElts);
Chris Lattner90951862006-04-16 00:51:47 +000010941
10942 }
10943 return true;
10944 }
10945 }
10946 }
10947 }
10948 }
10949 // TODO: Handle shufflevector here!
10950
10951 return false;
10952}
10953
10954/// CollectShuffleElements - We are building a shuffle of V, using RHS as the
10955/// RHS of the shuffle instruction, if it is not null. Return a shuffle mask
10956/// that computes V and the LHS value of the shuffle.
Chris Lattner39fac442006-04-15 01:39:45 +000010957static Value *CollectShuffleElements(Value *V, std::vector<Constant*> &Mask,
Chris Lattner90951862006-04-16 00:51:47 +000010958 Value *&RHS) {
Reid Spencerd84d35b2007-02-15 02:26:10 +000010959 assert(isa<VectorType>(V->getType()) &&
Chris Lattner90951862006-04-16 00:51:47 +000010960 (RHS == 0 || V->getType() == RHS->getType()) &&
Chris Lattner39fac442006-04-15 01:39:45 +000010961 "Invalid shuffle!");
Reid Spencerd84d35b2007-02-15 02:26:10 +000010962 unsigned NumElts = cast<VectorType>(V->getType())->getNumElements();
Chris Lattner39fac442006-04-15 01:39:45 +000010963
10964 if (isa<UndefValue>(V)) {
Reid Spencerc635f472006-12-31 05:48:39 +000010965 Mask.assign(NumElts, UndefValue::get(Type::Int32Ty));
Chris Lattner39fac442006-04-15 01:39:45 +000010966 return V;
10967 } else if (isa<ConstantAggregateZero>(V)) {
Reid Spencerc635f472006-12-31 05:48:39 +000010968 Mask.assign(NumElts, ConstantInt::get(Type::Int32Ty, 0));
Chris Lattner39fac442006-04-15 01:39:45 +000010969 return V;
10970 } else if (InsertElementInst *IEI = dyn_cast<InsertElementInst>(V)) {
10971 // If this is an insert of an extract from some other vector, include it.
10972 Value *VecOp = IEI->getOperand(0);
10973 Value *ScalarOp = IEI->getOperand(1);
10974 Value *IdxOp = IEI->getOperand(2);
10975
10976 if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)) {
10977 if (isa<ConstantInt>(EI->getOperand(1)) && isa<ConstantInt>(IdxOp) &&
10978 EI->getOperand(0)->getType() == V->getType()) {
10979 unsigned ExtractedIdx =
Reid Spencere0fc4df2006-10-20 07:07:24 +000010980 cast<ConstantInt>(EI->getOperand(1))->getZExtValue();
10981 unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue();
Chris Lattner39fac442006-04-15 01:39:45 +000010982
10983 // Either the extracted from or inserted into vector must be RHSVec,
10984 // otherwise we'd end up with a shuffle of three inputs.
Chris Lattner90951862006-04-16 00:51:47 +000010985 if (EI->getOperand(0) == RHS || RHS == 0) {
10986 RHS = EI->getOperand(0);
10987 Value *V = CollectShuffleElements(VecOp, Mask, RHS);
Chris Lattner39fac442006-04-15 01:39:45 +000010988 Mask[InsertedIdx & (NumElts-1)] =
Reid Spencerc635f472006-12-31 05:48:39 +000010989 ConstantInt::get(Type::Int32Ty, NumElts+ExtractedIdx);
Chris Lattner39fac442006-04-15 01:39:45 +000010990 return V;
10991 }
10992
Chris Lattner90951862006-04-16 00:51:47 +000010993 if (VecOp == RHS) {
10994 Value *V = CollectShuffleElements(EI->getOperand(0), Mask, RHS);
Chris Lattner39fac442006-04-15 01:39:45 +000010995 // Everything but the extracted element is replaced with the RHS.
10996 for (unsigned i = 0; i != NumElts; ++i) {
10997 if (i != InsertedIdx)
Reid Spencerc635f472006-12-31 05:48:39 +000010998 Mask[i] = ConstantInt::get(Type::Int32Ty, NumElts+i);
Chris Lattner39fac442006-04-15 01:39:45 +000010999 }
11000 return V;
11001 }
Chris Lattner90951862006-04-16 00:51:47 +000011002
11003 // If this insertelement is a chain that comes from exactly these two
11004 // vectors, return the vector and the effective shuffle.
11005 if (CollectSingleShuffleElements(IEI, EI->getOperand(0), RHS, Mask))
11006 return EI->getOperand(0);
11007
Chris Lattner39fac442006-04-15 01:39:45 +000011008 }
11009 }
11010 }
Chris Lattner90951862006-04-16 00:51:47 +000011011 // TODO: Handle shufflevector here!
Chris Lattner39fac442006-04-15 01:39:45 +000011012
11013 // Otherwise, can't do anything fancy. Return an identity vector.
11014 for (unsigned i = 0; i != NumElts; ++i)
Reid Spencerc635f472006-12-31 05:48:39 +000011015 Mask.push_back(ConstantInt::get(Type::Int32Ty, i));
Chris Lattner39fac442006-04-15 01:39:45 +000011016 return V;
11017}
11018
11019Instruction *InstCombiner::visitInsertElementInst(InsertElementInst &IE) {
11020 Value *VecOp = IE.getOperand(0);
11021 Value *ScalarOp = IE.getOperand(1);
11022 Value *IdxOp = IE.getOperand(2);
11023
Chris Lattner4ca9cbb2007-04-09 01:11:16 +000011024 // Inserting an undef or into an undefined place, remove this.
11025 if (isa<UndefValue>(ScalarOp) || isa<UndefValue>(IdxOp))
11026 ReplaceInstUsesWith(IE, VecOp);
11027
Chris Lattner39fac442006-04-15 01:39:45 +000011028 // If the inserted element was extracted from some other vector, and if the
11029 // indexes are constant, try to turn this into a shufflevector operation.
11030 if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)) {
11031 if (isa<ConstantInt>(EI->getOperand(1)) && isa<ConstantInt>(IdxOp) &&
11032 EI->getOperand(0)->getType() == IE.getType()) {
11033 unsigned NumVectorElts = IE.getType()->getNumElements();
Chris Lattner28d921d2007-04-14 23:32:02 +000011034 unsigned ExtractedIdx =
11035 cast<ConstantInt>(EI->getOperand(1))->getZExtValue();
Reid Spencere0fc4df2006-10-20 07:07:24 +000011036 unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue();
Chris Lattner39fac442006-04-15 01:39:45 +000011037
11038 if (ExtractedIdx >= NumVectorElts) // Out of range extract.
11039 return ReplaceInstUsesWith(IE, VecOp);
11040
11041 if (InsertedIdx >= NumVectorElts) // Out of range insert.
11042 return ReplaceInstUsesWith(IE, UndefValue::get(IE.getType()));
11043
11044 // If we are extracting a value from a vector, then inserting it right
11045 // back into the same place, just use the input vector.
11046 if (EI->getOperand(0) == VecOp && ExtractedIdx == InsertedIdx)
11047 return ReplaceInstUsesWith(IE, VecOp);
11048
11049 // We could theoretically do this for ANY input. However, doing so could
11050 // turn chains of insertelement instructions into a chain of shufflevector
11051 // instructions, and right now we do not merge shufflevectors. As such,
11052 // only do this in a situation where it is clear that there is benefit.
11053 if (isa<UndefValue>(VecOp) || isa<ConstantAggregateZero>(VecOp)) {
11054 // Turn this into shuffle(EIOp0, VecOp, Mask). The result has all of
11055 // the values of VecOp, except then one read from EIOp0.
11056 // Build a new shuffle mask.
11057 std::vector<Constant*> Mask;
11058 if (isa<UndefValue>(VecOp))
Reid Spencerc635f472006-12-31 05:48:39 +000011059 Mask.assign(NumVectorElts, UndefValue::get(Type::Int32Ty));
Chris Lattner39fac442006-04-15 01:39:45 +000011060 else {
11061 assert(isa<ConstantAggregateZero>(VecOp) && "Unknown thing");
Reid Spencerc635f472006-12-31 05:48:39 +000011062 Mask.assign(NumVectorElts, ConstantInt::get(Type::Int32Ty,
Chris Lattner39fac442006-04-15 01:39:45 +000011063 NumVectorElts));
11064 }
Reid Spencerc635f472006-12-31 05:48:39 +000011065 Mask[InsertedIdx] = ConstantInt::get(Type::Int32Ty, ExtractedIdx);
Chris Lattner39fac442006-04-15 01:39:45 +000011066 return new ShuffleVectorInst(EI->getOperand(0), VecOp,
Reid Spencerd84d35b2007-02-15 02:26:10 +000011067 ConstantVector::get(Mask));
Chris Lattner39fac442006-04-15 01:39:45 +000011068 }
11069
11070 // If this insertelement isn't used by some other insertelement, turn it
11071 // (and any insertelements it points to), into one big shuffle.
11072 if (!IE.hasOneUse() || !isa<InsertElementInst>(IE.use_back())) {
11073 std::vector<Constant*> Mask;
Chris Lattner90951862006-04-16 00:51:47 +000011074 Value *RHS = 0;
11075 Value *LHS = CollectShuffleElements(&IE, Mask, RHS);
11076 if (RHS == 0) RHS = UndefValue::get(LHS->getType());
11077 // We now have a shuffle of LHS, RHS, Mask.
Reid Spencerd84d35b2007-02-15 02:26:10 +000011078 return new ShuffleVectorInst(LHS, RHS, ConstantVector::get(Mask));
Chris Lattner39fac442006-04-15 01:39:45 +000011079 }
11080 }
11081 }
11082
11083 return 0;
11084}
11085
11086
Chris Lattnerfbb77a42006-04-10 22:45:52 +000011087Instruction *InstCombiner::visitShuffleVectorInst(ShuffleVectorInst &SVI) {
11088 Value *LHS = SVI.getOperand(0);
11089 Value *RHS = SVI.getOperand(1);
Chris Lattner12249be2006-05-25 23:48:38 +000011090 std::vector<unsigned> Mask = getShuffleMask(&SVI);
Chris Lattnerfbb77a42006-04-10 22:45:52 +000011091
11092 bool MadeChange = false;
11093
Chris Lattner2deeaea2006-10-05 06:55:50 +000011094 // Undefined shuffle mask -> undefined value.
Chris Lattner12249be2006-05-25 23:48:38 +000011095 if (isa<UndefValue>(SVI.getOperand(2)))
Chris Lattnerfbb77a42006-04-10 22:45:52 +000011096 return ReplaceInstUsesWith(SVI, UndefValue::get(SVI.getType()));
11097
Chris Lattnerd7b6ea12007-01-05 07:36:08 +000011098 // If we have shuffle(x, undef, mask) and any elements of mask refer to
Chris Lattner39fac442006-04-15 01:39:45 +000011099 // the undef, change them to undefs.
Chris Lattnerd7b6ea12007-01-05 07:36:08 +000011100 if (isa<UndefValue>(SVI.getOperand(1))) {
11101 // Scan to see if there are any references to the RHS. If so, replace them
11102 // with undef element refs and set MadeChange to true.
11103 for (unsigned i = 0, e = Mask.size(); i != e; ++i) {
11104 if (Mask[i] >= e && Mask[i] != 2*e) {
11105 Mask[i] = 2*e;
11106 MadeChange = true;
11107 }
11108 }
11109
11110 if (MadeChange) {
11111 // Remap any references to RHS to use LHS.
11112 std::vector<Constant*> Elts;
11113 for (unsigned i = 0, e = Mask.size(); i != e; ++i) {
11114 if (Mask[i] == 2*e)
11115 Elts.push_back(UndefValue::get(Type::Int32Ty));
11116 else
11117 Elts.push_back(ConstantInt::get(Type::Int32Ty, Mask[i]));
11118 }
Reid Spencerd84d35b2007-02-15 02:26:10 +000011119 SVI.setOperand(2, ConstantVector::get(Elts));
Chris Lattnerd7b6ea12007-01-05 07:36:08 +000011120 }
11121 }
Chris Lattner39fac442006-04-15 01:39:45 +000011122
Chris Lattner12249be2006-05-25 23:48:38 +000011123 // Canonicalize shuffle(x ,x,mask) -> shuffle(x, undef,mask')
11124 // Canonicalize shuffle(undef,x,mask) -> shuffle(x, undef,mask').
11125 if (LHS == RHS || isa<UndefValue>(LHS)) {
11126 if (isa<UndefValue>(LHS) && LHS == RHS) {
Chris Lattnerfbb77a42006-04-10 22:45:52 +000011127 // shuffle(undef,undef,mask) -> undef.
11128 return ReplaceInstUsesWith(SVI, LHS);
11129 }
11130
Chris Lattner12249be2006-05-25 23:48:38 +000011131 // Remap any references to RHS to use LHS.
11132 std::vector<Constant*> Elts;
11133 for (unsigned i = 0, e = Mask.size(); i != e; ++i) {
Chris Lattner0e477162006-05-26 00:29:06 +000011134 if (Mask[i] >= 2*e)
Reid Spencerc635f472006-12-31 05:48:39 +000011135 Elts.push_back(UndefValue::get(Type::Int32Ty));
Chris Lattner0e477162006-05-26 00:29:06 +000011136 else {
11137 if ((Mask[i] >= e && isa<UndefValue>(RHS)) ||
11138 (Mask[i] < e && isa<UndefValue>(LHS)))
11139 Mask[i] = 2*e; // Turn into undef.
11140 else
11141 Mask[i] &= (e-1); // Force to LHS.
Reid Spencerc635f472006-12-31 05:48:39 +000011142 Elts.push_back(ConstantInt::get(Type::Int32Ty, Mask[i]));
Chris Lattner0e477162006-05-26 00:29:06 +000011143 }
Chris Lattnerfbb77a42006-04-10 22:45:52 +000011144 }
Chris Lattner12249be2006-05-25 23:48:38 +000011145 SVI.setOperand(0, SVI.getOperand(1));
Chris Lattnerfbb77a42006-04-10 22:45:52 +000011146 SVI.setOperand(1, UndefValue::get(RHS->getType()));
Reid Spencerd84d35b2007-02-15 02:26:10 +000011147 SVI.setOperand(2, ConstantVector::get(Elts));
Chris Lattner0e477162006-05-26 00:29:06 +000011148 LHS = SVI.getOperand(0);
11149 RHS = SVI.getOperand(1);
Chris Lattnerfbb77a42006-04-10 22:45:52 +000011150 MadeChange = true;
11151 }
11152
Chris Lattner0e477162006-05-26 00:29:06 +000011153 // Analyze the shuffle, are the LHS or RHS and identity shuffles?
Chris Lattner12249be2006-05-25 23:48:38 +000011154 bool isLHSID = true, isRHSID = true;
Chris Lattner34cebe72006-04-16 00:03:56 +000011155
Chris Lattner12249be2006-05-25 23:48:38 +000011156 for (unsigned i = 0, e = Mask.size(); i != e; ++i) {
11157 if (Mask[i] >= e*2) continue; // Ignore undef values.
11158 // Is this an identity shuffle of the LHS value?
11159 isLHSID &= (Mask[i] == i);
11160
11161 // Is this an identity shuffle of the RHS value?
11162 isRHSID &= (Mask[i]-e == i);
Chris Lattner34cebe72006-04-16 00:03:56 +000011163 }
Chris Lattnerfbb77a42006-04-10 22:45:52 +000011164
Chris Lattner12249be2006-05-25 23:48:38 +000011165 // Eliminate identity shuffles.
11166 if (isLHSID) return ReplaceInstUsesWith(SVI, LHS);
11167 if (isRHSID) return ReplaceInstUsesWith(SVI, RHS);
Chris Lattnerfbb77a42006-04-10 22:45:52 +000011168
Chris Lattner0e477162006-05-26 00:29:06 +000011169 // If the LHS is a shufflevector itself, see if we can combine it with this
11170 // one without producing an unusual shuffle. Here we are really conservative:
11171 // we are absolutely afraid of producing a shuffle mask not in the input
11172 // program, because the code gen may not be smart enough to turn a merged
11173 // shuffle into two specific shuffles: it may produce worse code. As such,
11174 // we only merge two shuffles if the result is one of the two input shuffle
11175 // masks. In this case, merging the shuffles just removes one instruction,
11176 // which we know is safe. This is good for things like turning:
11177 // (splat(splat)) -> splat.
11178 if (ShuffleVectorInst *LHSSVI = dyn_cast<ShuffleVectorInst>(LHS)) {
11179 if (isa<UndefValue>(RHS)) {
11180 std::vector<unsigned> LHSMask = getShuffleMask(LHSSVI);
11181
11182 std::vector<unsigned> NewMask;
11183 for (unsigned i = 0, e = Mask.size(); i != e; ++i)
11184 if (Mask[i] >= 2*e)
11185 NewMask.push_back(2*e);
11186 else
11187 NewMask.push_back(LHSMask[Mask[i]]);
11188
11189 // If the result mask is equal to the src shuffle or this shuffle mask, do
11190 // the replacement.
11191 if (NewMask == LHSMask || NewMask == Mask) {
11192 std::vector<Constant*> Elts;
11193 for (unsigned i = 0, e = NewMask.size(); i != e; ++i) {
11194 if (NewMask[i] >= e*2) {
Reid Spencerc635f472006-12-31 05:48:39 +000011195 Elts.push_back(UndefValue::get(Type::Int32Ty));
Chris Lattner0e477162006-05-26 00:29:06 +000011196 } else {
Reid Spencerc635f472006-12-31 05:48:39 +000011197 Elts.push_back(ConstantInt::get(Type::Int32Ty, NewMask[i]));
Chris Lattner0e477162006-05-26 00:29:06 +000011198 }
11199 }
11200 return new ShuffleVectorInst(LHSSVI->getOperand(0),
11201 LHSSVI->getOperand(1),
Reid Spencerd84d35b2007-02-15 02:26:10 +000011202 ConstantVector::get(Elts));
Chris Lattner0e477162006-05-26 00:29:06 +000011203 }
11204 }
11205 }
Chris Lattner4284f642007-01-30 22:32:46 +000011206
Chris Lattnerfbb77a42006-04-10 22:45:52 +000011207 return MadeChange ? &SVI : 0;
11208}
11209
11210
Robert Bocchinoa8352962006-01-13 22:48:06 +000011211
Chris Lattner39c98bb2004-12-08 23:43:58 +000011212
11213/// TryToSinkInstruction - Try to move the specified instruction from its
11214/// current block into the beginning of DestBlock, which can only happen if it's
11215/// safe to move the instruction past all of the instructions between it and the
11216/// end of its block.
11217static bool TryToSinkInstruction(Instruction *I, BasicBlock *DestBlock) {
11218 assert(I->hasOneUse() && "Invariants didn't hold!");
11219
Chris Lattnerc4f67e62005-10-27 17:13:11 +000011220 // Cannot move control-flow-involving, volatile loads, vaarg, etc.
11221 if (isa<PHINode>(I) || I->mayWriteToMemory()) return false;
Misha Brukmanb1c93172005-04-21 23:48:37 +000011222
Chris Lattner39c98bb2004-12-08 23:43:58 +000011223 // Do not sink alloca instructions out of the entry block.
Dan Gohmandcb291f2007-03-22 16:38:57 +000011224 if (isa<AllocaInst>(I) && I->getParent() ==
11225 &DestBlock->getParent()->getEntryBlock())
Chris Lattner39c98bb2004-12-08 23:43:58 +000011226 return false;
11227
Chris Lattnerf17a2fb2004-12-09 07:14:34 +000011228 // We can only sink load instructions if there is nothing between the load and
11229 // the end of block that could change the value.
11230 if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
Chris Lattnerf17a2fb2004-12-09 07:14:34 +000011231 for (BasicBlock::iterator Scan = LI, E = LI->getParent()->end();
11232 Scan != E; ++Scan)
11233 if (Scan->mayWriteToMemory())
11234 return false;
Chris Lattnerf17a2fb2004-12-09 07:14:34 +000011235 }
Chris Lattner39c98bb2004-12-08 23:43:58 +000011236
11237 BasicBlock::iterator InsertPos = DestBlock->begin();
11238 while (isa<PHINode>(InsertPos)) ++InsertPos;
11239
Chris Lattner9f269e42005-08-08 19:11:57 +000011240 I->moveBefore(InsertPos);
Chris Lattner39c98bb2004-12-08 23:43:58 +000011241 ++NumSunkInst;
11242 return true;
11243}
11244
Chris Lattnera36ee4e2006-05-10 19:00:36 +000011245
11246/// AddReachableCodeToWorklist - Walk the function in depth-first order, adding
11247/// all reachable code to the worklist.
11248///
11249/// This has a couple of tricks to make the code faster and more powerful. In
11250/// particular, we constant fold and DCE instructions as we go, to avoid adding
11251/// them to the worklist (this significantly speeds up instcombine on code where
11252/// many instructions are dead or constant). Additionally, if we find a branch
11253/// whose condition is a known constant, we only visit the reachable successors.
11254///
11255static void AddReachableCodeToWorklist(BasicBlock *BB,
Chris Lattner7907e5f2007-02-15 19:41:52 +000011256 SmallPtrSet<BasicBlock*, 64> &Visited,
Chris Lattnerb15e2b12007-03-02 21:28:56 +000011257 InstCombiner &IC,
Chris Lattner1443bc52006-05-11 17:11:52 +000011258 const TargetData *TD) {
Chris Lattner12b89cc2007-03-23 19:17:18 +000011259 std::vector<BasicBlock*> Worklist;
11260 Worklist.push_back(BB);
Chris Lattnera36ee4e2006-05-10 19:00:36 +000011261
Chris Lattner12b89cc2007-03-23 19:17:18 +000011262 while (!Worklist.empty()) {
11263 BB = Worklist.back();
11264 Worklist.pop_back();
11265
11266 // We have now visited this block! If we've already been here, ignore it.
11267 if (!Visited.insert(BB)) continue;
11268
11269 for (BasicBlock::iterator BBI = BB->begin(), E = BB->end(); BBI != E; ) {
11270 Instruction *Inst = BBI++;
Chris Lattnera36ee4e2006-05-10 19:00:36 +000011271
Chris Lattner12b89cc2007-03-23 19:17:18 +000011272 // DCE instruction if trivially dead.
11273 if (isInstructionTriviallyDead(Inst)) {
11274 ++NumDeadInst;
11275 DOUT << "IC: DCE: " << *Inst;
11276 Inst->eraseFromParent();
11277 continue;
11278 }
11279
11280 // ConstantProp instruction if trivially constant.
11281 if (Constant *C = ConstantFoldInstruction(Inst, TD)) {
11282 DOUT << "IC: ConstFold to: " << *C << " from: " << *Inst;
11283 Inst->replaceAllUsesWith(C);
11284 ++NumConstProp;
11285 Inst->eraseFromParent();
11286 continue;
11287 }
Chris Lattnerd82e4a12007-07-20 22:06:41 +000011288
Chris Lattner12b89cc2007-03-23 19:17:18 +000011289 IC.AddToWorkList(Inst);
Chris Lattnera36ee4e2006-05-10 19:00:36 +000011290 }
Chris Lattner12b89cc2007-03-23 19:17:18 +000011291
11292 // Recursively visit successors. If this is a branch or switch on a
11293 // constant, only visit the reachable successor.
11294 TerminatorInst *TI = BB->getTerminator();
11295 if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
11296 if (BI->isConditional() && isa<ConstantInt>(BI->getCondition())) {
11297 bool CondVal = cast<ConstantInt>(BI->getCondition())->getZExtValue();
Nick Lewycky271506f2008-03-09 08:50:23 +000011298 BasicBlock *ReachableBB = BI->getSuccessor(!CondVal);
Nick Lewycky4d43d3c2008-04-25 16:53:59 +000011299 Worklist.push_back(ReachableBB);
Chris Lattner12b89cc2007-03-23 19:17:18 +000011300 continue;
11301 }
11302 } else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
11303 if (ConstantInt *Cond = dyn_cast<ConstantInt>(SI->getCondition())) {
11304 // See if this is an explicit destination.
11305 for (unsigned i = 1, e = SI->getNumSuccessors(); i != e; ++i)
11306 if (SI->getCaseValue(i) == Cond) {
Nick Lewycky271506f2008-03-09 08:50:23 +000011307 BasicBlock *ReachableBB = SI->getSuccessor(i);
Nick Lewycky4d43d3c2008-04-25 16:53:59 +000011308 Worklist.push_back(ReachableBB);
Chris Lattner12b89cc2007-03-23 19:17:18 +000011309 continue;
11310 }
11311
11312 // Otherwise it is the default destination.
11313 Worklist.push_back(SI->getSuccessor(0));
11314 continue;
11315 }
11316 }
11317
11318 for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
11319 Worklist.push_back(TI->getSuccessor(i));
Chris Lattnera36ee4e2006-05-10 19:00:36 +000011320 }
Chris Lattnera36ee4e2006-05-10 19:00:36 +000011321}
11322
Chris Lattner960a5432007-03-03 02:04:50 +000011323bool InstCombiner::DoOneIteration(Function &F, unsigned Iteration) {
Chris Lattner260ab202002-04-18 17:39:14 +000011324 bool Changed = false;
Chris Lattnerf4ad1652003-11-02 05:57:39 +000011325 TD = &getAnalysis<TargetData>();
Chris Lattner960a5432007-03-03 02:04:50 +000011326
11327 DEBUG(DOUT << "\n\nINSTCOMBINE ITERATION #" << Iteration << " on "
11328 << F.getNameStr() << "\n");
Chris Lattnerca081252001-12-14 16:52:21 +000011329
Chris Lattner4ed40f72005-07-07 20:40:38 +000011330 {
Chris Lattnera36ee4e2006-05-10 19:00:36 +000011331 // Do a depth-first traversal of the function, populate the worklist with
11332 // the reachable instructions. Ignore blocks that are not reachable. Keep
11333 // track of which blocks we visit.
Chris Lattner7907e5f2007-02-15 19:41:52 +000011334 SmallPtrSet<BasicBlock*, 64> Visited;
Chris Lattnerb15e2b12007-03-02 21:28:56 +000011335 AddReachableCodeToWorklist(F.begin(), Visited, *this, TD);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +000011336
Chris Lattner4ed40f72005-07-07 20:40:38 +000011337 // Do a quick scan over the function. If we find any blocks that are
11338 // unreachable, remove any instructions inside of them. This prevents
11339 // the instcombine code from having to deal with some bad special cases.
11340 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
11341 if (!Visited.count(BB)) {
11342 Instruction *Term = BB->getTerminator();
11343 while (Term != BB->begin()) { // Remove instrs bottom-up
11344 BasicBlock::iterator I = Term; --I;
Chris Lattner2d3a7a62004-04-27 15:13:33 +000011345
Bill Wendling5dbf43c2006-11-26 09:46:52 +000011346 DOUT << "IC: DCE: " << *I;
Chris Lattner4ed40f72005-07-07 20:40:38 +000011347 ++NumDeadInst;
11348
11349 if (!I->use_empty())
11350 I->replaceAllUsesWith(UndefValue::get(I->getType()));
11351 I->eraseFromParent();
11352 }
11353 }
11354 }
Chris Lattnerca081252001-12-14 16:52:21 +000011355
Chris Lattnerb15e2b12007-03-02 21:28:56 +000011356 while (!Worklist.empty()) {
11357 Instruction *I = RemoveOneFromWorkList();
11358 if (I == 0) continue; // skip null values.
Chris Lattnerca081252001-12-14 16:52:21 +000011359
Chris Lattner1443bc52006-05-11 17:11:52 +000011360 // Check to see if we can DCE the instruction.
Chris Lattner99f48c62002-09-02 04:59:56 +000011361 if (isInstructionTriviallyDead(I)) {
Chris Lattner1443bc52006-05-11 17:11:52 +000011362 // Add operands to the worklist.
Chris Lattnere8ed4ef2003-10-06 17:11:01 +000011363 if (I->getNumOperands() < 4)
Chris Lattner51ea1272004-02-28 05:22:00 +000011364 AddUsesToWorkList(*I);
Chris Lattner99f48c62002-09-02 04:59:56 +000011365 ++NumDeadInst;
Chris Lattnere8ed4ef2003-10-06 17:11:01 +000011366
Bill Wendling5dbf43c2006-11-26 09:46:52 +000011367 DOUT << "IC: DCE: " << *I;
Chris Lattnercd517ff2005-01-28 19:32:01 +000011368
11369 I->eraseFromParent();
Chris Lattnerb15e2b12007-03-02 21:28:56 +000011370 RemoveFromWorkList(I);
Chris Lattnere8ed4ef2003-10-06 17:11:01 +000011371 continue;
11372 }
Chris Lattner99f48c62002-09-02 04:59:56 +000011373
Chris Lattner1443bc52006-05-11 17:11:52 +000011374 // Instruction isn't dead, see if we can constant propagate it.
Chris Lattnere3eda252007-01-30 23:16:15 +000011375 if (Constant *C = ConstantFoldInstruction(I, TD)) {
Bill Wendling5dbf43c2006-11-26 09:46:52 +000011376 DOUT << "IC: ConstFold to: " << *C << " from: " << *I;
Chris Lattnercd517ff2005-01-28 19:32:01 +000011377
Chris Lattner1443bc52006-05-11 17:11:52 +000011378 // Add operands to the worklist.
Chris Lattner51ea1272004-02-28 05:22:00 +000011379 AddUsesToWorkList(*I);
Chris Lattnerc6509f42002-12-05 22:41:53 +000011380 ReplaceInstUsesWith(*I, C);
11381
Chris Lattner99f48c62002-09-02 04:59:56 +000011382 ++NumConstProp;
Chris Lattnera36ee4e2006-05-10 19:00:36 +000011383 I->eraseFromParent();
Chris Lattnerb15e2b12007-03-02 21:28:56 +000011384 RemoveFromWorkList(I);
Chris Lattnere8ed4ef2003-10-06 17:11:01 +000011385 continue;
Chris Lattner99f48c62002-09-02 04:59:56 +000011386 }
Chris Lattnere8ed4ef2003-10-06 17:11:01 +000011387
Chris Lattner39c98bb2004-12-08 23:43:58 +000011388 // See if we can trivially sink this instruction to a successor basic block.
11389 if (I->hasOneUse()) {
11390 BasicBlock *BB = I->getParent();
11391 BasicBlock *UserParent = cast<Instruction>(I->use_back())->getParent();
11392 if (UserParent != BB) {
11393 bool UserIsSuccessor = false;
11394 // See if the user is one of our successors.
11395 for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI)
11396 if (*SI == UserParent) {
11397 UserIsSuccessor = true;
11398 break;
11399 }
11400
11401 // If the user is one of our immediate successors, and if that successor
11402 // only has us as a predecessors (we'd have to split the critical edge
11403 // otherwise), we can keep going.
11404 if (UserIsSuccessor && !isa<PHINode>(I->use_back()) &&
11405 next(pred_begin(UserParent)) == pred_end(UserParent))
11406 // Okay, the CFG is simple enough, try to sink this instruction.
11407 Changed |= TryToSinkInstruction(I, UserParent);
11408 }
11409 }
11410
Chris Lattnerca081252001-12-14 16:52:21 +000011411 // Now that we have an instruction, try combining it to simplify it...
Reid Spencer755d0e72007-03-26 17:44:01 +000011412#ifndef NDEBUG
11413 std::string OrigI;
11414#endif
11415 DEBUG(std::ostringstream SS; I->print(SS); OrigI = SS.str(););
Chris Lattnerae7a0d32002-08-02 19:29:35 +000011416 if (Instruction *Result = visit(*I)) {
Chris Lattner0b18c1d2002-05-10 15:38:35 +000011417 ++NumCombined;
Chris Lattner260ab202002-04-18 17:39:14 +000011418 // Should we replace the old instruction with a new one?
Chris Lattner053c0932002-05-14 15:24:07 +000011419 if (Result != I) {
Bill Wendling5dbf43c2006-11-26 09:46:52 +000011420 DOUT << "IC: Old = " << *I
11421 << " New = " << *Result;
Chris Lattner7d2a5392004-03-13 23:54:27 +000011422
Chris Lattner396dbfe2004-06-09 05:08:07 +000011423 // Everything uses the new instruction now.
11424 I->replaceAllUsesWith(Result);
11425
11426 // Push the new instruction and any users onto the worklist.
Chris Lattnerb15e2b12007-03-02 21:28:56 +000011427 AddToWorkList(Result);
Chris Lattner396dbfe2004-06-09 05:08:07 +000011428 AddUsersToWorkList(*Result);
Chris Lattnere8ed4ef2003-10-06 17:11:01 +000011429
Chris Lattner6e0123b2007-02-11 01:23:03 +000011430 // Move the name to the new instruction first.
11431 Result->takeName(I);
Chris Lattnere8ed4ef2003-10-06 17:11:01 +000011432
11433 // Insert the new instruction into the basic block...
11434 BasicBlock *InstParent = I->getParent();
Chris Lattner7515cab2004-11-14 19:13:23 +000011435 BasicBlock::iterator InsertPos = I;
11436
11437 if (!isa<PHINode>(Result)) // If combining a PHI, don't insert
11438 while (isa<PHINode>(InsertPos)) // middle of a block of PHIs.
11439 ++InsertPos;
11440
11441 InstParent->getInstList().insert(InsertPos, Result);
Chris Lattnere8ed4ef2003-10-06 17:11:01 +000011442
Chris Lattner63d75af2004-05-01 23:27:23 +000011443 // Make sure that we reprocess all operands now that we reduced their
11444 // use counts.
Chris Lattnerb15e2b12007-03-02 21:28:56 +000011445 AddUsesToWorkList(*I);
Chris Lattnerb643a9e2004-05-01 23:19:52 +000011446
Chris Lattner396dbfe2004-06-09 05:08:07 +000011447 // Instructions can end up on the worklist more than once. Make sure
11448 // we do not process an instruction that has been deleted.
Chris Lattnerb15e2b12007-03-02 21:28:56 +000011449 RemoveFromWorkList(I);
Chris Lattnere8ed4ef2003-10-06 17:11:01 +000011450
11451 // Erase the old instruction.
11452 InstParent->getInstList().erase(I);
Chris Lattner113f4f42002-06-25 16:13:24 +000011453 } else {
Evan Chenga4ed8a52007-03-27 16:44:48 +000011454#ifndef NDEBUG
Reid Spencer755d0e72007-03-26 17:44:01 +000011455 DOUT << "IC: Mod = " << OrigI
11456 << " New = " << *I;
Evan Chenga4ed8a52007-03-27 16:44:48 +000011457#endif
Chris Lattner7d2a5392004-03-13 23:54:27 +000011458
Chris Lattnerae7a0d32002-08-02 19:29:35 +000011459 // If the instruction was modified, it's possible that it is now dead.
11460 // if so, remove it.
Chris Lattner63d75af2004-05-01 23:27:23 +000011461 if (isInstructionTriviallyDead(I)) {
11462 // Make sure we process all operands now that we are reducing their
11463 // use counts.
Chris Lattner960a5432007-03-03 02:04:50 +000011464 AddUsesToWorkList(*I);
Misha Brukmanb1c93172005-04-21 23:48:37 +000011465
Chris Lattner63d75af2004-05-01 23:27:23 +000011466 // Instructions may end up in the worklist more than once. Erase all
Robert Bocchinoa8352962006-01-13 22:48:06 +000011467 // occurrences of this instruction.
Chris Lattnerb15e2b12007-03-02 21:28:56 +000011468 RemoveFromWorkList(I);
Chris Lattner31f486c2005-01-31 05:36:43 +000011469 I->eraseFromParent();
Chris Lattner396dbfe2004-06-09 05:08:07 +000011470 } else {
Chris Lattner960a5432007-03-03 02:04:50 +000011471 AddToWorkList(I);
11472 AddUsersToWorkList(*I);
Chris Lattnerae7a0d32002-08-02 19:29:35 +000011473 }
Chris Lattner053c0932002-05-14 15:24:07 +000011474 }
Chris Lattner260ab202002-04-18 17:39:14 +000011475 Changed = true;
Chris Lattnerca081252001-12-14 16:52:21 +000011476 }
11477 }
11478
Chris Lattner960a5432007-03-03 02:04:50 +000011479 assert(WorklistMap.empty() && "Worklist empty, but map not?");
Chris Lattnerf0da7972007-08-05 08:47:58 +000011480
11481 // Do an explicit clear, this shrinks the map if needed.
11482 WorklistMap.clear();
Chris Lattner260ab202002-04-18 17:39:14 +000011483 return Changed;
Chris Lattner04805fa2002-02-26 21:46:54 +000011484}
11485
Chris Lattner960a5432007-03-03 02:04:50 +000011486
11487bool InstCombiner::runOnFunction(Function &F) {
Chris Lattner8258b442007-03-04 04:27:24 +000011488 MustPreserveLCSSA = mustPreserveAnalysisID(LCSSAID);
11489
Chris Lattner960a5432007-03-03 02:04:50 +000011490 bool EverMadeChange = false;
11491
11492 // Iterate while there is work to do.
11493 unsigned Iteration = 0;
11494 while (DoOneIteration(F, Iteration++))
11495 EverMadeChange = true;
11496 return EverMadeChange;
11497}
11498
Brian Gaeke38b79e82004-07-27 17:43:21 +000011499FunctionPass *llvm::createInstructionCombiningPass() {
Chris Lattner260ab202002-04-18 17:39:14 +000011500 return new InstCombiner();
Chris Lattner04805fa2002-02-26 21:46:54 +000011501}
Brian Gaeke960707c2003-11-11 22:41:34 +000011502