blob: 6b561f34af2b5e61aea3b3ebd1deb93b5342f56a [file] [log] [blame]
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001//===-- Instructions.cpp - Implement the LLVM instructions ----------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +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//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00008//===----------------------------------------------------------------------===//
9//
Chris Lattnerafdb3de2005-01-29 00:35:16 +000010// This file implements all of the non-inline methods for the LLVM instruction
11// classes.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000012//
13//===----------------------------------------------------------------------===//
14
Devang Pateladd58652009-09-23 18:32:25 +000015#include "LLVMContextImpl.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000016#include "llvm/Constants.h"
17#include "llvm/DerivedTypes.h"
18#include "llvm/Function.h"
19#include "llvm/Instructions.h"
Evan Cheng1d9d4bd2009-09-10 04:36:43 +000020#include "llvm/Module.h"
Dan Gohmand2a251f2009-07-17 21:33:58 +000021#include "llvm/Operator.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000022#include "llvm/Support/ErrorHandling.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000023#include "llvm/Support/CallSite.h"
Reid Spencer0286bc12007-02-28 22:00:54 +000024#include "llvm/Support/ConstantRange.h"
Christopher Lamb84485702007-04-22 19:24:39 +000025#include "llvm/Support/MathExtras.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000026using namespace llvm;
27
Chris Lattner3e13b8c2008-01-02 23:42:30 +000028//===----------------------------------------------------------------------===//
29// CallSite Class
30//===----------------------------------------------------------------------===//
31
Gabor Greifa2fbc0a2010-03-24 13:21:49 +000032User::op_iterator CallSite::getCallee() const {
33 Instruction *II(getInstruction());
34 return isCall()
Gabor Greif638c8232010-08-05 21:25:49 +000035 ? cast<CallInst>(II)->op_end() - 1 // Skip Callee
Gabor Greif6d673952010-07-16 09:38:02 +000036 : cast<InvokeInst>(II)->op_end() - 3; // Skip BB, BB, Callee
Gabor Greifa2fbc0a2010-03-24 13:21:49 +000037}
38
Gordon Henriksen14a55692007-12-10 02:14:30 +000039//===----------------------------------------------------------------------===//
40// TerminatorInst Class
41//===----------------------------------------------------------------------===//
42
43// Out of line virtual method, so the vtable, etc has a home.
44TerminatorInst::~TerminatorInst() {
45}
46
Gabor Greiff6caff662008-05-10 08:32:32 +000047//===----------------------------------------------------------------------===//
48// UnaryInstruction Class
49//===----------------------------------------------------------------------===//
50
Gordon Henriksen14a55692007-12-10 02:14:30 +000051// Out of line virtual method, so the vtable, etc has a home.
52UnaryInstruction::~UnaryInstruction() {
53}
54
Chris Lattnerafdb3de2005-01-29 00:35:16 +000055//===----------------------------------------------------------------------===//
Chris Lattner88107952008-12-29 00:12:50 +000056// SelectInst Class
57//===----------------------------------------------------------------------===//
58
59/// areInvalidOperands - Return a string if the specified operands are invalid
60/// for a select operation, otherwise return null.
61const char *SelectInst::areInvalidOperands(Value *Op0, Value *Op1, Value *Op2) {
62 if (Op1->getType() != Op2->getType())
63 return "both values to select must have same type";
64
65 if (const VectorType *VT = dyn_cast<VectorType>(Op0->getType())) {
66 // Vector select.
Owen Anderson55f1c092009-08-13 21:58:54 +000067 if (VT->getElementType() != Type::getInt1Ty(Op0->getContext()))
Chris Lattner88107952008-12-29 00:12:50 +000068 return "vector select condition element type must be i1";
69 const VectorType *ET = dyn_cast<VectorType>(Op1->getType());
70 if (ET == 0)
71 return "selected values for vector select must be vectors";
72 if (ET->getNumElements() != VT->getNumElements())
73 return "vector select requires selected vectors to have "
74 "the same vector length as select condition";
Owen Anderson55f1c092009-08-13 21:58:54 +000075 } else if (Op0->getType() != Type::getInt1Ty(Op0->getContext())) {
Chris Lattner88107952008-12-29 00:12:50 +000076 return "select condition must be i1 or <n x i1>";
77 }
78 return 0;
79}
80
81
82//===----------------------------------------------------------------------===//
Chris Lattnerafdb3de2005-01-29 00:35:16 +000083// PHINode Class
84//===----------------------------------------------------------------------===//
85
86PHINode::PHINode(const PHINode &PN)
87 : Instruction(PN.getType(), Instruction::PHI,
Gabor Greiff6caff662008-05-10 08:32:32 +000088 allocHungoffUses(PN.getNumOperands()), PN.getNumOperands()),
Chris Lattnerafdb3de2005-01-29 00:35:16 +000089 ReservedSpace(PN.getNumOperands()) {
90 Use *OL = OperandList;
91 for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +000092 OL[i] = PN.getOperand(i);
93 OL[i+1] = PN.getOperand(i+1);
Chris Lattnerafdb3de2005-01-29 00:35:16 +000094 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +000095 SubclassOptionalData = PN.SubclassOptionalData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +000096}
97
Gordon Henriksen14a55692007-12-10 02:14:30 +000098PHINode::~PHINode() {
Jay Foadbbb91f22011-01-16 15:30:52 +000099 dropHungoffUses();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000100}
101
102// removeIncomingValue - Remove an incoming value. This is useful if a
103// predecessor basic block is deleted.
104Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
105 unsigned NumOps = getNumOperands();
106 Use *OL = OperandList;
107 assert(Idx*2 < NumOps && "BB not in PHI node!");
108 Value *Removed = OL[Idx*2];
109
110 // Move everything after this operand down.
111 //
112 // FIXME: we could just swap with the end of the list, then erase. However,
113 // client might not expect this to happen. The code as it is thrashes the
114 // use/def lists, which is kinda lame.
115 for (unsigned i = (Idx+1)*2; i != NumOps; i += 2) {
116 OL[i-2] = OL[i];
117 OL[i-2+1] = OL[i+1];
118 }
119
120 // Nuke the last value.
121 OL[NumOps-2].set(0);
122 OL[NumOps-2+1].set(0);
123 NumOperands = NumOps-2;
124
125 // If the PHI node is dead, because it has zero entries, nuke it now.
126 if (NumOps == 2 && DeletePHIIfEmpty) {
127 // If anyone is using this PHI, make them use a dummy value instead...
Owen Andersonb292b8c2009-07-30 23:03:37 +0000128 replaceAllUsesWith(UndefValue::get(getType()));
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000129 eraseFromParent();
130 }
131 return Removed;
132}
133
134/// resizeOperands - resize operands - This adjusts the length of the operands
135/// list according to the following behavior:
136/// 1. If NumOps == 0, grow the operand list in response to a push_back style
137/// of operation. This grows the number of ops by 1.5 times.
138/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
139/// 3. If NumOps == NumOperands, trim the reserved space.
140///
141void PHINode::resizeOperands(unsigned NumOps) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000142 unsigned e = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000143 if (NumOps == 0) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000144 NumOps = e*3/2;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000145 if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common.
146 } else if (NumOps*2 > NumOperands) {
147 // No resize needed.
148 if (ReservedSpace >= NumOps) return;
149 } else if (NumOps == NumOperands) {
150 if (ReservedSpace == NumOps) return;
151 } else {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000152 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000153 }
154
155 ReservedSpace = NumOps;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000156 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +0000157 Use *NewOps = allocHungoffUses(NumOps);
Dan Gohman031f0bb2008-06-23 16:45:24 +0000158 std::copy(OldOps, OldOps + e, NewOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000159 OperandList = NewOps;
Jay Foadbbb91f22011-01-16 15:30:52 +0000160 Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000161}
162
Nate Begemanb3923212005-08-04 23:24:19 +0000163/// hasConstantValue - If the specified PHI node always merges together the same
164/// value, return the value, otherwise return null.
Duncan Sands7412f6e2010-11-17 04:30:22 +0000165Value *PHINode::hasConstantValue() const {
166 // Exploit the fact that phi nodes always have at least one entry.
167 Value *ConstantValue = getIncomingValue(0);
168 for (unsigned i = 1, e = getNumIncomingValues(); i != e; ++i)
169 if (getIncomingValue(i) != ConstantValue)
170 return 0; // Incoming values not all the same.
171 return ConstantValue;
Nate Begemanb3923212005-08-04 23:24:19 +0000172}
173
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000174
175//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000176// CallInst Implementation
177//===----------------------------------------------------------------------===//
178
Gordon Henriksen14a55692007-12-10 02:14:30 +0000179CallInst::~CallInst() {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000180}
181
Chris Lattner054ba2c2007-02-13 00:58:44 +0000182void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000183 assert(NumOperands == NumParams+1 && "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000184 Op<-1>() = Func;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000185
Misha Brukmanb1c93172005-04-21 23:48:37 +0000186 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000187 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +0000188 (void)FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000189
Chris Lattner054ba2c2007-02-13 00:58:44 +0000190 assert((NumParams == FTy->getNumParams() ||
191 (FTy->isVarArg() && NumParams > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000192 "Calling a function with bad signature!");
Chris Lattner054ba2c2007-02-13 00:58:44 +0000193 for (unsigned i = 0; i != NumParams; ++i) {
Chris Lattner667a0562006-05-03 00:48:22 +0000194 assert((i >= FTy->getNumParams() ||
195 FTy->getParamType(i) == Params[i]->getType()) &&
196 "Calling a function with a bad signature!");
Gabor Greif6d673952010-07-16 09:38:02 +0000197 OperandList[i] = Params[i];
Chris Lattner667a0562006-05-03 00:48:22 +0000198 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000199}
200
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000201void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000202 assert(NumOperands == 3 && "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000203 Op<-1>() = Func;
204 Op<0>() = Actual1;
205 Op<1>() = Actual2;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000206
207 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000208 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +0000209 (void)FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000210
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000211 assert((FTy->getNumParams() == 2 ||
Chris Lattner667a0562006-05-03 00:48:22 +0000212 (FTy->isVarArg() && FTy->getNumParams() < 2)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000213 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000214 assert((0 >= FTy->getNumParams() ||
215 FTy->getParamType(0) == Actual1->getType()) &&
216 "Calling a function with a bad signature!");
217 assert((1 >= FTy->getNumParams() ||
218 FTy->getParamType(1) == Actual2->getType()) &&
219 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000220}
221
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000222void CallInst::init(Value *Func, Value *Actual) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000223 assert(NumOperands == 2 && "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000224 Op<-1>() = Func;
225 Op<0>() = Actual;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000226
227 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000228 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +0000229 (void)FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000230
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000231 assert((FTy->getNumParams() == 1 ||
232 (FTy->isVarArg() && FTy->getNumParams() == 0)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000233 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000234 assert((0 == FTy->getNumParams() ||
235 FTy->getParamType(0) == Actual->getType()) &&
236 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000237}
238
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000239void CallInst::init(Value *Func) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000240 assert(NumOperands == 1 && "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000241 Op<-1>() = Func;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000242
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000243 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000244 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +0000245 (void)FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000246
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000247 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000248}
249
Daniel Dunbar4975db62009-07-25 04:41:11 +0000250CallInst::CallInst(Value *Func, Value* Actual, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +0000251 Instruction *InsertBefore)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000252 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
253 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000254 Instruction::Call,
255 OperandTraits<CallInst>::op_end(this) - 2,
256 2, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000257 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000258 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000259}
260
Daniel Dunbar4975db62009-07-25 04:41:11 +0000261CallInst::CallInst(Value *Func, Value* Actual, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000262 BasicBlock *InsertAtEnd)
263 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
264 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000265 Instruction::Call,
266 OperandTraits<CallInst>::op_end(this) - 2,
267 2, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000268 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000269 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000270}
Daniel Dunbar4975db62009-07-25 04:41:11 +0000271CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000272 Instruction *InsertBefore)
273 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
274 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000275 Instruction::Call,
276 OperandTraits<CallInst>::op_end(this) - 1,
277 1, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000278 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000279 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000280}
281
Daniel Dunbar4975db62009-07-25 04:41:11 +0000282CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000283 BasicBlock *InsertAtEnd)
284 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
285 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000286 Instruction::Call,
287 OperandTraits<CallInst>::op_end(this) - 1,
288 1, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000289 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000290 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000291}
292
Misha Brukmanb1c93172005-04-21 23:48:37 +0000293CallInst::CallInst(const CallInst &CI)
Gabor Greiff6caff662008-05-10 08:32:32 +0000294 : Instruction(CI.getType(), Instruction::Call,
295 OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(),
Chris Lattner8a923e72008-03-12 17:45:29 +0000296 CI.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000297 setAttributes(CI.getAttributes());
Chris Lattnerb9c86512009-12-29 02:14:09 +0000298 setTailCall(CI.isTailCall());
299 setCallingConv(CI.getCallingConv());
300
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000301 Use *OL = OperandList;
302 Use *InOL = CI.OperandList;
303 for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000304 OL[i] = InOL[i];
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000305 SubclassOptionalData = CI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000306}
307
Devang Patel4c758ea2008-09-25 21:00:45 +0000308void CallInst::addAttribute(unsigned i, Attributes attr) {
309 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000310 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000311 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000312}
313
Devang Patel4c758ea2008-09-25 21:00:45 +0000314void CallInst::removeAttribute(unsigned i, Attributes attr) {
315 AttrListPtr PAL = getAttributes();
Duncan Sands78c88722008-07-08 08:38:44 +0000316 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000317 setAttributes(PAL);
Duncan Sands78c88722008-07-08 08:38:44 +0000318}
319
Devang Patelba3fa6c2008-09-23 23:03:40 +0000320bool CallInst::paramHasAttr(unsigned i, Attributes attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000321 if (AttributeList.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000322 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000323 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000324 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000325 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000326}
327
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000328/// IsConstantOne - Return true only if val is constant int 1
329static bool IsConstantOne(Value *val) {
330 assert(val && "IsConstantOne does not work with NULL val");
331 return isa<ConstantInt>(val) && cast<ConstantInt>(val)->isOne();
332}
333
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000334static Instruction *createMalloc(Instruction *InsertBefore,
335 BasicBlock *InsertAtEnd, const Type *IntPtrTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000336 const Type *AllocTy, Value *AllocSize,
337 Value *ArraySize, Function *MallocF,
338 const Twine &Name) {
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000339 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
Victor Hernandez788eaab2009-09-18 19:20:02 +0000340 "createMalloc needs either InsertBefore or InsertAtEnd");
341
342 // malloc(type) becomes:
343 // bitcast (i8* malloc(typeSize)) to type*
344 // malloc(type, arraySize) becomes:
345 // bitcast (i8 *malloc(typeSize*arraySize)) to type*
Victor Hernandezf3db9152009-11-07 00:16:28 +0000346 if (!ArraySize)
347 ArraySize = ConstantInt::get(IntPtrTy, 1);
348 else if (ArraySize->getType() != IntPtrTy) {
349 if (InsertBefore)
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000350 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
351 "", InsertBefore);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000352 else
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000353 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
354 "", InsertAtEnd);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000355 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000356
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000357 if (!IsConstantOne(ArraySize)) {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000358 if (IsConstantOne(AllocSize)) {
359 AllocSize = ArraySize; // Operand * 1 = Operand
360 } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) {
361 Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy,
362 false /*ZExt*/);
363 // Malloc arg is constant product of type size and array size
364 AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize));
365 } else {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000366 // Multiply type size by the array size...
367 if (InsertBefore)
Victor Hernandez788eaab2009-09-18 19:20:02 +0000368 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
369 "mallocsize", InsertBefore);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000370 else
Victor Hernandez788eaab2009-09-18 19:20:02 +0000371 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
372 "mallocsize", InsertAtEnd);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000373 }
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000374 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000375
Victor Hernandez788eaab2009-09-18 19:20:02 +0000376 assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size");
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000377 // Create the call to Malloc.
378 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
379 Module* M = BB->getParent()->getParent();
Duncan Sands9ed7b162009-10-06 15:40:36 +0000380 const Type *BPTy = Type::getInt8PtrTy(BB->getContext());
Victor Hernandezbb336a12009-11-10 19:53:28 +0000381 Value *MallocFunc = MallocF;
382 if (!MallocFunc)
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000383 // prototype malloc as "void *malloc(size_t)"
Victor Hernandezbb336a12009-11-10 19:53:28 +0000384 MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy, NULL);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000385 const PointerType *AllocPtrType = PointerType::getUnqual(AllocTy);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000386 CallInst *MCall = NULL;
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000387 Instruction *Result = NULL;
Victor Hernandez788eaab2009-09-18 19:20:02 +0000388 if (InsertBefore) {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000389 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall", InsertBefore);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000390 Result = MCall;
391 if (Result->getType() != AllocPtrType)
392 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000393 Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000394 } else {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000395 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall");
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000396 Result = MCall;
397 if (Result->getType() != AllocPtrType) {
398 InsertAtEnd->getInstList().push_back(MCall);
399 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000400 Result = new BitCastInst(MCall, AllocPtrType, Name);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000401 }
Victor Hernandez788eaab2009-09-18 19:20:02 +0000402 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000403 MCall->setTailCall();
Victor Hernandezbb336a12009-11-10 19:53:28 +0000404 if (Function *F = dyn_cast<Function>(MallocFunc)) {
405 MCall->setCallingConv(F->getCallingConv());
406 if (!F->doesNotAlias(0)) F->setDoesNotAlias(0);
407 }
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000408 assert(!MCall->getType()->isVoidTy() && "Malloc has void return type");
Victor Hernandez788eaab2009-09-18 19:20:02 +0000409
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000410 return Result;
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000411}
412
413/// CreateMalloc - Generate the IR for a call to malloc:
414/// 1. Compute the malloc call's argument as the specified type's size,
415/// possibly multiplied by the array size if the array size is not
416/// constant 1.
417/// 2. Call malloc with that argument.
418/// 3. Bitcast the result of the malloc call to the specified type.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000419Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
420 const Type *IntPtrTy, const Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000421 Value *AllocSize, Value *ArraySize,
Duncan Sands41b4a6b2010-07-12 08:16:59 +0000422 Function * MallocF,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000423 const Twine &Name) {
424 return createMalloc(InsertBefore, NULL, IntPtrTy, AllocTy, AllocSize,
Chris Lattner601e390a2010-07-12 00:57:28 +0000425 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000426}
427
428/// CreateMalloc - Generate the IR for a call to malloc:
429/// 1. Compute the malloc call's argument as the specified type's size,
430/// possibly multiplied by the array size if the array size is not
431/// constant 1.
432/// 2. Call malloc with that argument.
433/// 3. Bitcast the result of the malloc call to the specified type.
434/// Note: This function does not add the bitcast to the basic block, that is the
435/// responsibility of the caller.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000436Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
437 const Type *IntPtrTy, const Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000438 Value *AllocSize, Value *ArraySize,
439 Function *MallocF, const Twine &Name) {
440 return createMalloc(NULL, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000441 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000442}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000443
Victor Hernandeze2971492009-10-24 04:23:03 +0000444static Instruction* createFree(Value* Source, Instruction *InsertBefore,
445 BasicBlock *InsertAtEnd) {
446 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
447 "createFree needs either InsertBefore or InsertAtEnd");
Duncan Sands19d0b472010-02-16 11:11:14 +0000448 assert(Source->getType()->isPointerTy() &&
Victor Hernandeze2971492009-10-24 04:23:03 +0000449 "Can not free something of nonpointer type!");
450
451 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
452 Module* M = BB->getParent()->getParent();
453
454 const Type *VoidTy = Type::getVoidTy(M->getContext());
455 const Type *IntPtrTy = Type::getInt8PtrTy(M->getContext());
456 // prototype free as "void free(void*)"
Chris Lattner2156c222009-11-09 07:12:01 +0000457 Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy, NULL);
Victor Hernandeze2971492009-10-24 04:23:03 +0000458 CallInst* Result = NULL;
459 Value *PtrCast = Source;
460 if (InsertBefore) {
461 if (Source->getType() != IntPtrTy)
462 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertBefore);
463 Result = CallInst::Create(FreeFunc, PtrCast, "", InsertBefore);
464 } else {
465 if (Source->getType() != IntPtrTy)
466 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd);
467 Result = CallInst::Create(FreeFunc, PtrCast, "");
468 }
469 Result->setTailCall();
Chris Lattner2156c222009-11-09 07:12:01 +0000470 if (Function *F = dyn_cast<Function>(FreeFunc))
471 Result->setCallingConv(F->getCallingConv());
Victor Hernandeze2971492009-10-24 04:23:03 +0000472
473 return Result;
474}
475
476/// CreateFree - Generate the IR for a call to the builtin free function.
Chris Lattner601e390a2010-07-12 00:57:28 +0000477Instruction * CallInst::CreateFree(Value* Source, Instruction *InsertBefore) {
478 return createFree(Source, InsertBefore, NULL);
Victor Hernandeze2971492009-10-24 04:23:03 +0000479}
480
481/// CreateFree - Generate the IR for a call to the builtin free function.
482/// Note: This function does not add the call to the basic block, that is the
483/// responsibility of the caller.
484Instruction* CallInst::CreateFree(Value* Source, BasicBlock *InsertAtEnd) {
485 Instruction* FreeCall = createFree(Source, NULL, InsertAtEnd);
486 assert(FreeCall && "CreateFree did not create a CallInst");
487 return FreeCall;
488}
489
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000490//===----------------------------------------------------------------------===//
491// InvokeInst Implementation
492//===----------------------------------------------------------------------===//
493
494void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000495 Value* const *Args, unsigned NumArgs) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000496 assert(NumOperands == 3+NumArgs && "NumOperands not set up?");
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000497 Op<-3>() = Fn;
498 Op<-2>() = IfNormal;
499 Op<-1>() = IfException;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000500 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000501 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +0000502 (void)FTy; // silence warning.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000503
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000504 assert(((NumArgs == FTy->getNumParams()) ||
505 (FTy->isVarArg() && NumArgs > FTy->getNumParams())) &&
Gabor Greif668d7002010-03-23 13:45:54 +0000506 "Invoking a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000507
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000508 Use *OL = OperandList;
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000509 for (unsigned i = 0, e = NumArgs; i != e; i++) {
Chris Lattner667a0562006-05-03 00:48:22 +0000510 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000511 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000512 "Invoking a function with a bad signature!");
513
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000514 OL[i] = Args[i];
Chris Lattner667a0562006-05-03 00:48:22 +0000515 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000516}
517
Misha Brukmanb1c93172005-04-21 23:48:37 +0000518InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000519 : TerminatorInst(II.getType(), Instruction::Invoke,
Gabor Greif697e94c2008-05-15 10:04:30 +0000520 OperandTraits<InvokeInst>::op_end(this)
521 - II.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000522 II.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000523 setAttributes(II.getAttributes());
Chris Lattnerb9c86512009-12-29 02:14:09 +0000524 setCallingConv(II.getCallingConv());
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000525 Use *OL = OperandList, *InOL = II.OperandList;
526 for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000527 OL[i] = InOL[i];
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000528 SubclassOptionalData = II.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000529}
530
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000531BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
532 return getSuccessor(idx);
533}
534unsigned InvokeInst::getNumSuccessorsV() const {
535 return getNumSuccessors();
536}
537void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
538 return setSuccessor(idx, B);
539}
540
Devang Patelba3fa6c2008-09-23 23:03:40 +0000541bool InvokeInst::paramHasAttr(unsigned i, Attributes attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000542 if (AttributeList.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000543 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000544 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000545 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000546 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000547}
548
Devang Patel4c758ea2008-09-25 21:00:45 +0000549void InvokeInst::addAttribute(unsigned i, Attributes attr) {
550 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000551 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000552 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000553}
554
Devang Patel4c758ea2008-09-25 21:00:45 +0000555void InvokeInst::removeAttribute(unsigned i, Attributes attr) {
556 AttrListPtr PAL = getAttributes();
Duncan Sands78c88722008-07-08 08:38:44 +0000557 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000558 setAttributes(PAL);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000559}
560
Duncan Sands5208d1a2007-11-28 17:07:01 +0000561
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000562//===----------------------------------------------------------------------===//
563// ReturnInst Implementation
564//===----------------------------------------------------------------------===//
565
Chris Lattner2195fc42007-02-24 00:55:48 +0000566ReturnInst::ReturnInst(const ReturnInst &RI)
Owen Anderson55f1c092009-08-13 21:58:54 +0000567 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000568 OperandTraits<ReturnInst>::op_end(this) -
569 RI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000570 RI.getNumOperands()) {
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000571 if (RI.getNumOperands())
Gabor Greif2d3024d2008-05-26 21:33:52 +0000572 Op<0>() = RI.Op<0>();
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000573 SubclassOptionalData = RI.SubclassOptionalData;
Chris Lattner2195fc42007-02-24 00:55:48 +0000574}
575
Owen Anderson55f1c092009-08-13 21:58:54 +0000576ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore)
577 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000578 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
579 InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000580 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000581 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000582}
Owen Anderson55f1c092009-08-13 21:58:54 +0000583ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd)
584 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000585 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
586 InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000587 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000588 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000589}
Owen Anderson55f1c092009-08-13 21:58:54 +0000590ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
591 : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000592 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
Devang Patel59643e52008-02-23 00:35:18 +0000593}
594
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000595unsigned ReturnInst::getNumSuccessorsV() const {
596 return getNumSuccessors();
597}
598
Devang Patelae682fb2008-02-26 17:56:20 +0000599/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
600/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000601void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000602 llvm_unreachable("ReturnInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000603}
604
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000605BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000606 llvm_unreachable("ReturnInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000607 return 0;
608}
609
Devang Patel59643e52008-02-23 00:35:18 +0000610ReturnInst::~ReturnInst() {
Devang Patel59643e52008-02-23 00:35:18 +0000611}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000612
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000613//===----------------------------------------------------------------------===//
614// UnwindInst Implementation
615//===----------------------------------------------------------------------===//
616
Owen Anderson55f1c092009-08-13 21:58:54 +0000617UnwindInst::UnwindInst(LLVMContext &Context, Instruction *InsertBefore)
618 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unwind,
619 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000620}
Owen Anderson55f1c092009-08-13 21:58:54 +0000621UnwindInst::UnwindInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
622 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unwind,
623 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000624}
625
626
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000627unsigned UnwindInst::getNumSuccessorsV() const {
628 return getNumSuccessors();
629}
630
631void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000632 llvm_unreachable("UnwindInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000633}
634
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000635BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000636 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000637 return 0;
638}
639
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000640//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000641// UnreachableInst Implementation
642//===----------------------------------------------------------------------===//
643
Owen Anderson55f1c092009-08-13 21:58:54 +0000644UnreachableInst::UnreachableInst(LLVMContext &Context,
645 Instruction *InsertBefore)
646 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
647 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000648}
Owen Anderson55f1c092009-08-13 21:58:54 +0000649UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
650 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
651 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000652}
653
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000654unsigned UnreachableInst::getNumSuccessorsV() const {
655 return getNumSuccessors();
656}
657
658void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000659 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000660}
661
662BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000663 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000664 return 0;
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000665}
666
667//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000668// BranchInst Implementation
669//===----------------------------------------------------------------------===//
670
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000671void BranchInst::AssertOK() {
672 if (isConditional())
Duncan Sands9dff9be2010-02-15 16:12:20 +0000673 assert(getCondition()->getType()->isIntegerTy(1) &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000674 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000675}
676
Chris Lattner2195fc42007-02-24 00:55:48 +0000677BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000678 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000679 OperandTraits<BranchInst>::op_end(this) - 1,
680 1, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000681 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000682 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000683}
684BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
685 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000686 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000687 OperandTraits<BranchInst>::op_end(this) - 3,
688 3, InsertBefore) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000689 Op<-1>() = IfTrue;
690 Op<-2>() = IfFalse;
691 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000692#ifndef NDEBUG
693 AssertOK();
694#endif
695}
696
697BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000698 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000699 OperandTraits<BranchInst>::op_end(this) - 1,
700 1, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000701 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000702 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000703}
704
705BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
706 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000707 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000708 OperandTraits<BranchInst>::op_end(this) - 3,
709 3, InsertAtEnd) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000710 Op<-1>() = IfTrue;
711 Op<-2>() = IfFalse;
712 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000713#ifndef NDEBUG
714 AssertOK();
715#endif
716}
717
718
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000719BranchInst::BranchInst(const BranchInst &BI) :
Owen Anderson55f1c092009-08-13 21:58:54 +0000720 TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000721 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
722 BI.getNumOperands()) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000723 Op<-1>() = BI.Op<-1>();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000724 if (BI.getNumOperands() != 1) {
725 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000726 Op<-3>() = BI.Op<-3>();
727 Op<-2>() = BI.Op<-2>();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000728 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000729 SubclassOptionalData = BI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000730}
731
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000732BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
733 return getSuccessor(idx);
734}
735unsigned BranchInst::getNumSuccessorsV() const {
736 return getNumSuccessors();
737}
738void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
739 setSuccessor(idx, B);
740}
741
742
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000743//===----------------------------------------------------------------------===//
Victor Hernandez8acf2952009-10-23 21:09:37 +0000744// AllocaInst Implementation
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000745//===----------------------------------------------------------------------===//
746
Owen Andersonb6b25302009-07-14 23:09:55 +0000747static Value *getAISize(LLVMContext &Context, Value *Amt) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000748 if (!Amt)
Owen Anderson55f1c092009-08-13 21:58:54 +0000749 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000750 else {
751 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +0000752 "Passed basic block into allocation size parameter! Use other ctor");
Dan Gohman2140a742010-05-28 01:14:11 +0000753 assert(Amt->getType()->isIntegerTy() &&
754 "Allocation array size is not an integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000755 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000756 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000757}
758
Victor Hernandez8acf2952009-10-23 21:09:37 +0000759AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize,
760 const Twine &Name, Instruction *InsertBefore)
761 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
762 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
763 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000764 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000765 setName(Name);
766}
767
768AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize,
769 const Twine &Name, BasicBlock *InsertAtEnd)
770 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
771 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
772 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000773 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000774 setName(Name);
775}
776
777AllocaInst::AllocaInst(const Type *Ty, const Twine &Name,
778 Instruction *InsertBefore)
779 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
780 getAISize(Ty->getContext(), 0), InsertBefore) {
781 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000782 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000783 setName(Name);
784}
785
786AllocaInst::AllocaInst(const Type *Ty, const Twine &Name,
787 BasicBlock *InsertAtEnd)
788 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
789 getAISize(Ty->getContext(), 0), InsertAtEnd) {
790 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000791 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000792 setName(Name);
793}
794
795AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
796 const Twine &Name, Instruction *InsertBefore)
797 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
Owen Anderson4fdeba92009-07-15 23:53:25 +0000798 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000799 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000800 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000801 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000802}
803
Victor Hernandez8acf2952009-10-23 21:09:37 +0000804AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
805 const Twine &Name, BasicBlock *InsertAtEnd)
806 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
Owen Anderson4fdeba92009-07-15 23:53:25 +0000807 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000808 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000809 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000810 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000811}
812
Gordon Henriksen14a55692007-12-10 02:14:30 +0000813// Out of line virtual method, so the vtable, etc has a home.
Victor Hernandez8acf2952009-10-23 21:09:37 +0000814AllocaInst::~AllocaInst() {
Gordon Henriksen14a55692007-12-10 02:14:30 +0000815}
816
Victor Hernandez8acf2952009-10-23 21:09:37 +0000817void AllocaInst::setAlignment(unsigned Align) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000818 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +0000819 assert(Align <= MaximumAlignment &&
820 "Alignment is greater than MaximumAlignment!");
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +0000821 setInstructionSubclassData(Log2_32(Align) + 1);
Dan Gohmanaa583d72008-03-24 16:55:58 +0000822 assert(getAlignment() == Align && "Alignment representation error!");
823}
824
Victor Hernandez8acf2952009-10-23 21:09:37 +0000825bool AllocaInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +0000826 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
Dan Gohman9a1b8592010-09-27 15:15:44 +0000827 return !CI->isOne();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000828 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000829}
830
Victor Hernandez8acf2952009-10-23 21:09:37 +0000831const Type *AllocaInst::getAllocatedType() const {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000832 return getType()->getElementType();
833}
834
Chris Lattner8b291e62008-11-26 02:54:17 +0000835/// isStaticAlloca - Return true if this alloca is in the entry block of the
836/// function and is a constant size. If so, the code generator will fold it
837/// into the prolog/epilog code, so it is basically free.
838bool AllocaInst::isStaticAlloca() const {
839 // Must be constant size.
840 if (!isa<ConstantInt>(getArraySize())) return false;
841
842 // Must be in the entry block.
843 const BasicBlock *Parent = getParent();
844 return Parent == &Parent->getParent()->front();
845}
846
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000847//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000848// LoadInst Implementation
849//===----------------------------------------------------------------------===//
850
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000851void LoadInst::AssertOK() {
Duncan Sands19d0b472010-02-16 11:11:14 +0000852 assert(getOperand(0)->getType()->isPointerTy() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000853 "Ptr must have pointer type.");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000854}
855
Daniel Dunbar4975db62009-07-25 04:41:11 +0000856LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000857 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000858 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000859 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000860 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000861 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000862 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000863}
864
Daniel Dunbar4975db62009-07-25 04:41:11 +0000865LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000866 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000867 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000868 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000869 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000870 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000871 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000872}
873
Daniel Dunbar4975db62009-07-25 04:41:11 +0000874LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000875 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000876 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000877 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000878 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000879 setAlignment(0);
880 AssertOK();
881 setName(Name);
882}
883
Daniel Dunbar4975db62009-07-25 04:41:11 +0000884LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Christopher Lamb84485702007-04-22 19:24:39 +0000885 unsigned Align, Instruction *InsertBef)
886 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
887 Load, Ptr, InsertBef) {
888 setVolatile(isVolatile);
889 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000890 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000891 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000892}
893
Daniel Dunbar4975db62009-07-25 04:41:11 +0000894LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +0000895 unsigned Align, BasicBlock *InsertAE)
896 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
897 Load, Ptr, InsertAE) {
898 setVolatile(isVolatile);
899 setAlignment(Align);
900 AssertOK();
901 setName(Name);
902}
903
Daniel Dunbar4975db62009-07-25 04:41:11 +0000904LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000905 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000906 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000907 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000908 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000909 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000910 AssertOK();
911 setName(Name);
912}
913
Daniel Dunbar27096822009-08-11 18:11:15 +0000914
915
916LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
917 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
918 Load, Ptr, InsertBef) {
919 setVolatile(false);
920 setAlignment(0);
921 AssertOK();
922 if (Name && Name[0]) setName(Name);
923}
924
925LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
926 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
927 Load, Ptr, InsertAE) {
928 setVolatile(false);
929 setAlignment(0);
930 AssertOK();
931 if (Name && Name[0]) setName(Name);
932}
933
934LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
935 Instruction *InsertBef)
936: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
937 Load, Ptr, InsertBef) {
938 setVolatile(isVolatile);
939 setAlignment(0);
940 AssertOK();
941 if (Name && Name[0]) setName(Name);
942}
943
944LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
945 BasicBlock *InsertAE)
946 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
947 Load, Ptr, InsertAE) {
948 setVolatile(isVolatile);
949 setAlignment(0);
950 AssertOK();
951 if (Name && Name[0]) setName(Name);
952}
953
Christopher Lamb84485702007-04-22 19:24:39 +0000954void LoadInst::setAlignment(unsigned Align) {
955 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +0000956 assert(Align <= MaximumAlignment &&
957 "Alignment is greater than MaximumAlignment!");
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +0000958 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
959 ((Log2_32(Align)+1)<<1));
Dan Gohmana7e5a242010-07-28 20:12:04 +0000960 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +0000961}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000962
963//===----------------------------------------------------------------------===//
964// StoreInst Implementation
965//===----------------------------------------------------------------------===//
966
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000967void StoreInst::AssertOK() {
Nate Begemanfecbc8c2008-07-29 15:49:41 +0000968 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Duncan Sands19d0b472010-02-16 11:11:14 +0000969 assert(getOperand(1)->getType()->isPointerTy() &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000970 "Ptr must have pointer type!");
971 assert(getOperand(0)->getType() ==
972 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +0000973 && "Ptr must be a pointer to Val type!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000974}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000975
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000976
977StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000978 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +0000979 OperandTraits<StoreInst>::op_begin(this),
980 OperandTraits<StoreInst>::operands(this),
981 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000982 Op<0>() = val;
983 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +0000984 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000985 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000986 AssertOK();
987}
988
989StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000990 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +0000991 OperandTraits<StoreInst>::op_begin(this),
992 OperandTraits<StoreInst>::operands(this),
993 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000994 Op<0>() = val;
995 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +0000996 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000997 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000998 AssertOK();
999}
1000
Misha Brukmanb1c93172005-04-21 23:48:37 +00001001StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001002 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001003 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001004 OperandTraits<StoreInst>::op_begin(this),
1005 OperandTraits<StoreInst>::operands(this),
1006 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001007 Op<0>() = val;
1008 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001009 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001010 setAlignment(0);
1011 AssertOK();
1012}
1013
1014StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1015 unsigned Align, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001016 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001017 OperandTraits<StoreInst>::op_begin(this),
1018 OperandTraits<StoreInst>::operands(this),
1019 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001020 Op<0>() = val;
1021 Op<1>() = addr;
Christopher Lamb84485702007-04-22 19:24:39 +00001022 setVolatile(isVolatile);
1023 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001024 AssertOK();
1025}
1026
Misha Brukmanb1c93172005-04-21 23:48:37 +00001027StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +00001028 unsigned Align, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001029 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001030 OperandTraits<StoreInst>::op_begin(this),
1031 OperandTraits<StoreInst>::operands(this),
1032 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001033 Op<0>() = val;
1034 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +00001035 setVolatile(isVolatile);
1036 setAlignment(Align);
1037 AssertOK();
1038}
1039
1040StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001041 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001042 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001043 OperandTraits<StoreInst>::op_begin(this),
1044 OperandTraits<StoreInst>::operands(this),
1045 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001046 Op<0>() = val;
1047 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001048 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001049 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001050 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001051}
1052
Christopher Lamb84485702007-04-22 19:24:39 +00001053void StoreInst::setAlignment(unsigned Align) {
1054 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001055 assert(Align <= MaximumAlignment &&
1056 "Alignment is greater than MaximumAlignment!");
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001057 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
1058 ((Log2_32(Align)+1) << 1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001059 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001060}
1061
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001062//===----------------------------------------------------------------------===//
1063// GetElementPtrInst Implementation
1064//===----------------------------------------------------------------------===//
1065
Christopher Lambedf07882007-12-17 01:12:55 +00001066static unsigned retrieveAddrSpace(const Value *Val) {
1067 return cast<PointerType>(Val->getType())->getAddressSpace();
1068}
1069
Gabor Greif21ba1842008-06-06 20:28:12 +00001070void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001071 const Twine &Name) {
Gabor Greiff6caff662008-05-10 08:32:32 +00001072 assert(NumOperands == 1+NumIdx && "NumOperands not initialized?");
1073 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +00001074 OL[0] = Ptr;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001075
Chris Lattner79807c3d2007-01-31 19:47:18 +00001076 for (unsigned i = 0; i != NumIdx; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +00001077 OL[i+1] = Idx[i];
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001078
1079 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001080}
1081
Daniel Dunbar4975db62009-07-25 04:41:11 +00001082void GetElementPtrInst::init(Value *Ptr, Value *Idx, const Twine &Name) {
Gabor Greiff6caff662008-05-10 08:32:32 +00001083 assert(NumOperands == 2 && "NumOperands not initialized?");
1084 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +00001085 OL[0] = Ptr;
1086 OL[1] = Idx;
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001087
1088 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001089}
1090
Gabor Greiff6caff662008-05-10 08:32:32 +00001091GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
Gabor Greife9408e62008-05-27 11:03:29 +00001092 : Instruction(GEPI.getType(), GetElementPtr,
Gabor Greif697e94c2008-05-15 10:04:30 +00001093 OperandTraits<GetElementPtrInst>::op_end(this)
1094 - GEPI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001095 GEPI.getNumOperands()) {
1096 Use *OL = OperandList;
1097 Use *GEPIOL = GEPI.OperandList;
1098 for (unsigned i = 0, E = NumOperands; i != E; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +00001099 OL[i] = GEPIOL[i];
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001100 SubclassOptionalData = GEPI.SubclassOptionalData;
Gabor Greiff6caff662008-05-10 08:32:32 +00001101}
1102
Chris Lattner82981202005-05-03 05:43:30 +00001103GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001104 const Twine &Name, Instruction *InBe)
Owen Anderson4056ca92009-07-29 22:17:13 +00001105 : Instruction(PointerType::get(
Owen Andersond420fd42009-07-16 00:03:07 +00001106 checkType(getIndexedType(Ptr->getType(),Idx)), retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001107 GetElementPtr,
1108 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1109 2, InBe) {
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001110 init(Ptr, Idx, Name);
Chris Lattner82981202005-05-03 05:43:30 +00001111}
1112
1113GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001114 const Twine &Name, BasicBlock *IAE)
Owen Anderson4056ca92009-07-29 22:17:13 +00001115 : Instruction(PointerType::get(
Owen Andersond420fd42009-07-16 00:03:07 +00001116 checkType(getIndexedType(Ptr->getType(),Idx)),
1117 retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001118 GetElementPtr,
1119 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1120 2, IAE) {
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001121 init(Ptr, Idx, Name);
Chris Lattner82981202005-05-03 05:43:30 +00001122}
1123
Chris Lattner6090a422009-03-09 04:46:40 +00001124/// getIndexedType - Returns the type of the element that would be accessed with
1125/// a gep instruction with the specified parameters.
1126///
1127/// The Idxs pointer should point to a continuous piece of memory containing the
1128/// indices, either as Value* or uint64_t.
1129///
1130/// A null type is returned if the indices are invalid for the specified
1131/// pointer type.
1132///
Matthijs Kooijman04468622008-07-29 08:46:11 +00001133template <typename IndexTy>
Chris Lattner6090a422009-03-09 04:46:40 +00001134static const Type* getIndexedTypeInternal(const Type *Ptr, IndexTy const *Idxs,
1135 unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00001136 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1137 if (!PTy) return 0; // Type isn't a pointer type!
1138 const Type *Agg = PTy->getElementType();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001139
Chris Lattner6090a422009-03-09 04:46:40 +00001140 // Handle the special case of the empty set index set, which is always valid.
Dan Gohman12fce772008-05-15 19:50:34 +00001141 if (NumIdx == 0)
1142 return Agg;
Chris Lattner6090a422009-03-09 04:46:40 +00001143
1144 // If there is at least one index, the top level type must be sized, otherwise
Chris Lattner4a488152009-03-09 04:56:22 +00001145 // it cannot be 'stepped over'. We explicitly allow abstract types (those
1146 // that contain opaque types) under the assumption that it will be resolved to
1147 // a sane type later.
1148 if (!Agg->isSized() && !Agg->isAbstract())
Chris Lattner6090a422009-03-09 04:46:40 +00001149 return 0;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001150
Dan Gohman1ecaf452008-05-31 00:58:22 +00001151 unsigned CurIdx = 1;
1152 for (; CurIdx != NumIdx; ++CurIdx) {
1153 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
Duncan Sands19d0b472010-02-16 11:11:14 +00001154 if (!CT || CT->isPointerTy()) return 0;
Matthijs Kooijman04468622008-07-29 08:46:11 +00001155 IndexTy Index = Idxs[CurIdx];
Dan Gohman1ecaf452008-05-31 00:58:22 +00001156 if (!CT->indexValid(Index)) return 0;
1157 Agg = CT->getTypeAtIndex(Index);
1158
1159 // If the new type forwards to another type, then it is in the middle
1160 // of being refined to another type (and hence, may have dropped all
1161 // references to what it was using before). So, use the new forwarded
1162 // type.
1163 if (const Type *Ty = Agg->getForwardedType())
1164 Agg = Ty;
1165 }
1166 return CurIdx == NumIdx ? Agg : 0;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001167}
1168
Matthijs Kooijman04468622008-07-29 08:46:11 +00001169const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
1170 Value* const *Idxs,
1171 unsigned NumIdx) {
1172 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1173}
1174
1175const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
Jay Foad1d4a8fe2011-01-14 08:07:43 +00001176 Constant* const *Idxs,
1177 unsigned NumIdx) {
1178 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1179}
1180
1181const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
Matthijs Kooijman04468622008-07-29 08:46:11 +00001182 uint64_t const *Idxs,
1183 unsigned NumIdx) {
1184 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1185}
1186
Chris Lattner82981202005-05-03 05:43:30 +00001187const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
1188 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1189 if (!PTy) return 0; // Type isn't a pointer type!
1190
1191 // Check the pointer index.
1192 if (!PTy->indexValid(Idx)) return 0;
1193
Chris Lattnerc2233332005-05-03 16:44:45 +00001194 return PTy->getElementType();
Chris Lattner82981202005-05-03 05:43:30 +00001195}
1196
Chris Lattner45f15572007-04-14 00:12:57 +00001197
1198/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1199/// zeros. If so, the result pointer and the first operand have the same
1200/// value, just potentially different types.
1201bool GetElementPtrInst::hasAllZeroIndices() const {
1202 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1203 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1204 if (!CI->isZero()) return false;
1205 } else {
1206 return false;
1207 }
1208 }
1209 return true;
1210}
1211
Chris Lattner27058292007-04-27 20:35:56 +00001212/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1213/// constant integers. If so, the result pointer and the first operand have
1214/// a constant offset between them.
1215bool GetElementPtrInst::hasAllConstantIndices() const {
1216 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1217 if (!isa<ConstantInt>(getOperand(i)))
1218 return false;
1219 }
1220 return true;
1221}
1222
Dan Gohman1b849082009-09-07 23:54:19 +00001223void GetElementPtrInst::setIsInBounds(bool B) {
1224 cast<GEPOperator>(this)->setIsInBounds(B);
1225}
Chris Lattner45f15572007-04-14 00:12:57 +00001226
Nick Lewycky28a5f252009-09-27 21:33:04 +00001227bool GetElementPtrInst::isInBounds() const {
1228 return cast<GEPOperator>(this)->isInBounds();
1229}
1230
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001231//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001232// ExtractElementInst Implementation
1233//===----------------------------------------------------------------------===//
1234
1235ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001236 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001237 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001238 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001239 ExtractElement,
1240 OperandTraits<ExtractElementInst>::op_begin(this),
1241 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001242 assert(isValidOperands(Val, Index) &&
1243 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001244 Op<0>() = Val;
1245 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001246 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001247}
1248
1249ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001250 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001251 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001252 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001253 ExtractElement,
1254 OperandTraits<ExtractElementInst>::op_begin(this),
1255 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001256 assert(isValidOperands(Val, Index) &&
1257 "Invalid extractelement instruction operands!");
1258
Gabor Greif2d3024d2008-05-26 21:33:52 +00001259 Op<0>() = Val;
1260 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001261 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001262}
1263
Chris Lattner65511ff2006-10-05 06:24:58 +00001264
Chris Lattner54865b32006-04-08 04:05:48 +00001265bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001266 if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy(32))
Chris Lattner54865b32006-04-08 04:05:48 +00001267 return false;
1268 return true;
1269}
1270
1271
Robert Bocchino23004482006-01-10 19:05:34 +00001272//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001273// InsertElementInst Implementation
1274//===----------------------------------------------------------------------===//
1275
Chris Lattner54865b32006-04-08 04:05:48 +00001276InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001277 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001278 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001279 : Instruction(Vec->getType(), InsertElement,
1280 OperandTraits<InsertElementInst>::op_begin(this),
1281 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001282 assert(isValidOperands(Vec, Elt, Index) &&
1283 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001284 Op<0>() = Vec;
1285 Op<1>() = Elt;
1286 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001287 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001288}
1289
Chris Lattner54865b32006-04-08 04:05:48 +00001290InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001291 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001292 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001293 : Instruction(Vec->getType(), InsertElement,
1294 OperandTraits<InsertElementInst>::op_begin(this),
1295 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001296 assert(isValidOperands(Vec, Elt, Index) &&
1297 "Invalid insertelement instruction operands!");
1298
Gabor Greif2d3024d2008-05-26 21:33:52 +00001299 Op<0>() = Vec;
1300 Op<1>() = Elt;
1301 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001302 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001303}
1304
Chris Lattner54865b32006-04-08 04:05:48 +00001305bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1306 const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001307 if (!Vec->getType()->isVectorTy())
Reid Spencer09575ba2007-02-15 03:39:18 +00001308 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001309
Reid Spencerd84d35b2007-02-15 02:26:10 +00001310 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001311 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001312
Duncan Sands9dff9be2010-02-15 16:12:20 +00001313 if (!Index->getType()->isIntegerTy(32))
Dan Gohman4fe64de2009-06-14 23:30:43 +00001314 return false; // Third operand of insertelement must be i32.
Chris Lattner54865b32006-04-08 04:05:48 +00001315 return true;
1316}
1317
1318
Robert Bocchinoca27f032006-01-17 20:07:22 +00001319//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001320// ShuffleVectorInst Implementation
1321//===----------------------------------------------------------------------===//
1322
1323ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001324 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001325 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +00001326: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
Mon P Wang25f01062008-11-10 04:46:22 +00001327 cast<VectorType>(Mask->getType())->getNumElements()),
1328 ShuffleVector,
1329 OperandTraits<ShuffleVectorInst>::op_begin(this),
1330 OperandTraits<ShuffleVectorInst>::operands(this),
1331 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001332 assert(isValidOperands(V1, V2, Mask) &&
1333 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001334 Op<0>() = V1;
1335 Op<1>() = V2;
1336 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001337 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001338}
1339
1340ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001341 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001342 BasicBlock *InsertAtEnd)
Dan Gohmane5af8cd2009-08-25 23:27:45 +00001343: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1344 cast<VectorType>(Mask->getType())->getNumElements()),
1345 ShuffleVector,
1346 OperandTraits<ShuffleVectorInst>::op_begin(this),
1347 OperandTraits<ShuffleVectorInst>::operands(this),
1348 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001349 assert(isValidOperands(V1, V2, Mask) &&
1350 "Invalid shuffle vector instruction operands!");
1351
Gabor Greif2d3024d2008-05-26 21:33:52 +00001352 Op<0>() = V1;
1353 Op<1>() = V2;
1354 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001355 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001356}
1357
Mon P Wang25f01062008-11-10 04:46:22 +00001358bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001359 const Value *Mask) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001360 if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001361 return false;
1362
1363 const VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
Nate Begeman60a31c32010-08-13 00:16:46 +00001364 if (MaskTy == 0 || !MaskTy->getElementType()->isIntegerTy(32))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001365 return false;
Nate Begeman60a31c32010-08-13 00:16:46 +00001366
1367 // Check to see if Mask is valid.
1368 if (const ConstantVector *MV = dyn_cast<ConstantVector>(Mask)) {
1369 const VectorType *VTy = cast<VectorType>(V1->getType());
1370 for (unsigned i = 0, e = MV->getNumOperands(); i != e; ++i) {
1371 if (ConstantInt* CI = dyn_cast<ConstantInt>(MV->getOperand(i))) {
1372 if (CI->uge(VTy->getNumElements()*2))
1373 return false;
1374 } else if (!isa<UndefValue>(MV->getOperand(i))) {
1375 return false;
1376 }
1377 }
1378 }
1379 else if (!isa<UndefValue>(Mask) && !isa<ConstantAggregateZero>(Mask))
1380 return false;
1381
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001382 return true;
1383}
1384
Chris Lattnerf724e342008-03-02 05:28:33 +00001385/// getMaskValue - Return the index from the shuffle mask for the specified
1386/// output result. This is either -1 if the element is undef or a number less
1387/// than 2*numelements.
1388int ShuffleVectorInst::getMaskValue(unsigned i) const {
1389 const Constant *Mask = cast<Constant>(getOperand(2));
1390 if (isa<UndefValue>(Mask)) return -1;
1391 if (isa<ConstantAggregateZero>(Mask)) return 0;
1392 const ConstantVector *MaskCV = cast<ConstantVector>(Mask);
1393 assert(i < MaskCV->getNumOperands() && "Index out of range");
1394
1395 if (isa<UndefValue>(MaskCV->getOperand(i)))
1396 return -1;
1397 return cast<ConstantInt>(MaskCV->getOperand(i))->getZExtValue();
1398}
1399
Dan Gohman12fce772008-05-15 19:50:34 +00001400//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001401// InsertValueInst Class
1402//===----------------------------------------------------------------------===//
1403
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001404void InsertValueInst::init(Value *Agg, Value *Val, const unsigned *Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001405 unsigned NumIdx, const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001406 assert(NumOperands == 2 && "NumOperands not initialized?");
Frits van Bommel16ebe772010-12-05 20:50:26 +00001407 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idx, Idx + NumIdx) ==
1408 Val->getType() && "Inserted value must match indexed type!");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001409 Op<0>() = Agg;
1410 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001411
Dan Gohmandd41bba2010-06-21 19:47:52 +00001412 Indices.append(Idx, Idx + NumIdx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001413 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001414}
1415
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001416void InsertValueInst::init(Value *Agg, Value *Val, unsigned Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001417 const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001418 assert(NumOperands == 2 && "NumOperands not initialized?");
Frits van Bommel16ebe772010-12-05 20:50:26 +00001419 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idx) == Val->getType()
1420 && "Inserted value must match indexed type!");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001421 Op<0>() = Agg;
1422 Op<1>() = Val;
1423
1424 Indices.push_back(Idx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001425 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001426}
1427
1428InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001429 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001430 OperandTraits<InsertValueInst>::op_begin(this), 2),
1431 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001432 Op<0>() = IVI.getOperand(0);
1433 Op<1>() = IVI.getOperand(1);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001434 SubclassOptionalData = IVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001435}
1436
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001437InsertValueInst::InsertValueInst(Value *Agg,
1438 Value *Val,
1439 unsigned Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001440 const Twine &Name,
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001441 Instruction *InsertBefore)
1442 : Instruction(Agg->getType(), InsertValue,
1443 OperandTraits<InsertValueInst>::op_begin(this),
1444 2, InsertBefore) {
1445 init(Agg, Val, Idx, Name);
1446}
1447
1448InsertValueInst::InsertValueInst(Value *Agg,
1449 Value *Val,
1450 unsigned Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001451 const Twine &Name,
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001452 BasicBlock *InsertAtEnd)
1453 : Instruction(Agg->getType(), InsertValue,
1454 OperandTraits<InsertValueInst>::op_begin(this),
1455 2, InsertAtEnd) {
1456 init(Agg, Val, Idx, Name);
1457}
1458
Dan Gohman0752bff2008-05-23 00:36:11 +00001459//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001460// ExtractValueInst Class
1461//===----------------------------------------------------------------------===//
1462
Gabor Greifcbcc4952008-06-06 21:06:32 +00001463void ExtractValueInst::init(const unsigned *Idx, unsigned NumIdx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001464 const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001465 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00001466
Dan Gohmandd41bba2010-06-21 19:47:52 +00001467 Indices.append(Idx, Idx + NumIdx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001468 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001469}
1470
Daniel Dunbar4975db62009-07-25 04:41:11 +00001471void ExtractValueInst::init(unsigned Idx, const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001472 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001473
1474 Indices.push_back(Idx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001475 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001476}
1477
1478ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00001479 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00001480 Indices(EVI.Indices) {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001481 SubclassOptionalData = EVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001482}
1483
Dan Gohman12fce772008-05-15 19:50:34 +00001484// getIndexedType - Returns the type of the element that would be extracted
1485// with an extractvalue instruction with the specified parameters.
1486//
1487// A null type is returned if the indices are invalid for the specified
1488// pointer type.
1489//
1490const Type* ExtractValueInst::getIndexedType(const Type *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001491 const unsigned *Idxs,
Dan Gohman12fce772008-05-15 19:50:34 +00001492 unsigned NumIdx) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001493 for (unsigned CurIdx = 0; CurIdx != NumIdx; ++CurIdx) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001494 unsigned Index = Idxs[CurIdx];
Frits van Bommel16ebe772010-12-05 20:50:26 +00001495 // We can't use CompositeType::indexValid(Index) here.
1496 // indexValid() always returns true for arrays because getelementptr allows
1497 // out-of-bounds indices. Since we don't allow those for extractvalue and
1498 // insertvalue we need to check array indexing manually.
1499 // Since the only other types we can index into are struct types it's just
1500 // as easy to check those manually as well.
1501 if (const ArrayType *AT = dyn_cast<ArrayType>(Agg)) {
1502 if (Index >= AT->getNumElements())
1503 return 0;
1504 } else if (const StructType *ST = dyn_cast<StructType>(Agg)) {
1505 if (Index >= ST->getNumElements())
1506 return 0;
1507 } else {
1508 // Not a valid type to index into.
1509 return 0;
1510 }
1511
1512 Agg = cast<CompositeType>(Agg)->getTypeAtIndex(Index);
Dan Gohman12fce772008-05-15 19:50:34 +00001513
1514 // If the new type forwards to another type, then it is in the middle
1515 // of being refined to another type (and hence, may have dropped all
1516 // references to what it was using before). So, use the new forwarded
1517 // type.
1518 if (const Type *Ty = Agg->getForwardedType())
1519 Agg = Ty;
1520 }
Frits van Bommel16ebe772010-12-05 20:50:26 +00001521 return Agg;
Dan Gohman12fce772008-05-15 19:50:34 +00001522}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001523
Dan Gohman4a3125b2008-06-17 21:07:55 +00001524const Type* ExtractValueInst::getIndexedType(const Type *Agg,
Dan Gohmand87e8132008-06-20 00:47:44 +00001525 unsigned Idx) {
1526 return getIndexedType(Agg, &Idx, 1);
Dan Gohman4a3125b2008-06-17 21:07:55 +00001527}
1528
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001529//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001530// BinaryOperator Class
1531//===----------------------------------------------------------------------===//
1532
Chris Lattner2195fc42007-02-24 00:55:48 +00001533BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001534 const Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001535 Instruction *InsertBefore)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001536 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001537 OperandTraits<BinaryOperator>::op_begin(this),
1538 OperandTraits<BinaryOperator>::operands(this),
1539 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001540 Op<0>() = S1;
1541 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001542 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001543 setName(Name);
1544}
1545
1546BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001547 const Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001548 BasicBlock *InsertAtEnd)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001549 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001550 OperandTraits<BinaryOperator>::op_begin(this),
1551 OperandTraits<BinaryOperator>::operands(this),
1552 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001553 Op<0>() = S1;
1554 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001555 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001556 setName(Name);
1557}
1558
1559
1560void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001561 Value *LHS = getOperand(0), *RHS = getOperand(1);
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +00001562 (void)LHS; (void)RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001563 assert(LHS->getType() == RHS->getType() &&
1564 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001565#ifndef NDEBUG
1566 switch (iType) {
1567 case Add: case Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001568 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001569 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001570 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001571 assert(getType()->isIntOrIntVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001572 "Tried to create an integer operation on a non-integer type!");
1573 break;
1574 case FAdd: case FSub:
1575 case FMul:
1576 assert(getType() == LHS->getType() &&
1577 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001578 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001579 "Tried to create a floating-point operation on a "
1580 "non-floating-point type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001581 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001582 case UDiv:
1583 case SDiv:
1584 assert(getType() == LHS->getType() &&
1585 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001586 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001587 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001588 "Incorrect operand type (not integer) for S/UDIV");
1589 break;
1590 case FDiv:
1591 assert(getType() == LHS->getType() &&
1592 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001593 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001594 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001595 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001596 case URem:
1597 case SRem:
1598 assert(getType() == LHS->getType() &&
1599 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001600 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001601 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001602 "Incorrect operand type (not integer) for S/UREM");
1603 break;
1604 case FRem:
1605 assert(getType() == LHS->getType() &&
1606 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001607 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001608 "Incorrect operand type (not floating point) for FREM");
Reid Spencer7eb55b32006-11-02 01:53:59 +00001609 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001610 case Shl:
1611 case LShr:
1612 case AShr:
1613 assert(getType() == LHS->getType() &&
1614 "Shift operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001615 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00001616 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001617 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001618 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00001619 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001620 case And: case Or:
1621 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001622 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001623 "Logical operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001624 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00001625 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001626 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001627 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001628 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001629 default:
1630 break;
1631 }
1632#endif
1633}
1634
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001635BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001636 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001637 Instruction *InsertBefore) {
1638 assert(S1->getType() == S2->getType() &&
1639 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001640 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001641}
1642
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001643BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001644 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001645 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001646 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001647 InsertAtEnd->getInstList().push_back(Res);
1648 return Res;
1649}
1650
Dan Gohman5476cfd2009-08-12 16:23:25 +00001651BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001652 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001653 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001654 return new BinaryOperator(Instruction::Sub,
1655 zero, Op,
1656 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001657}
1658
Dan Gohman5476cfd2009-08-12 16:23:25 +00001659BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001660 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001661 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001662 return new BinaryOperator(Instruction::Sub,
1663 zero, Op,
1664 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001665}
1666
Dan Gohman4ab44202009-12-18 02:58:50 +00001667BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
1668 Instruction *InsertBefore) {
1669 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1670 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore);
1671}
1672
1673BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
1674 BasicBlock *InsertAtEnd) {
1675 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1676 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd);
1677}
1678
Duncan Sandsfa5f5962010-02-02 12:53:04 +00001679BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
1680 Instruction *InsertBefore) {
1681 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1682 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore);
1683}
1684
1685BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
1686 BasicBlock *InsertAtEnd) {
1687 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1688 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd);
1689}
1690
Dan Gohman5476cfd2009-08-12 16:23:25 +00001691BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001692 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001693 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Dan Gohmana5b96452009-06-04 22:49:04 +00001694 return new BinaryOperator(Instruction::FSub,
1695 zero, Op,
1696 Op->getType(), Name, InsertBefore);
1697}
1698
Dan Gohman5476cfd2009-08-12 16:23:25 +00001699BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001700 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001701 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Dan Gohmana5b96452009-06-04 22:49:04 +00001702 return new BinaryOperator(Instruction::FSub,
1703 zero, Op,
1704 Op->getType(), Name, InsertAtEnd);
1705}
1706
Dan Gohman5476cfd2009-08-12 16:23:25 +00001707BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001708 Instruction *InsertBefore) {
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001709 Constant *C;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001710 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001711 C = Constant::getAllOnesValue(PTy->getElementType());
Owen Anderson4aa32952009-07-28 21:19:26 +00001712 C = ConstantVector::get(
Owen Andersonf945a9e2009-07-15 21:51:10 +00001713 std::vector<Constant*>(PTy->getNumElements(), C));
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001714 } else {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001715 C = Constant::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001716 }
1717
1718 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001719 Op->getType(), Name, InsertBefore);
1720}
1721
Dan Gohman5476cfd2009-08-12 16:23:25 +00001722BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001723 BasicBlock *InsertAtEnd) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001724 Constant *AllOnes;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001725 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001726 // Create a vector of all ones values.
Owen Anderson5a1acd92009-07-31 20:28:14 +00001727 Constant *Elt = Constant::getAllOnesValue(PTy->getElementType());
Owen Anderson4aa32952009-07-28 21:19:26 +00001728 AllOnes = ConstantVector::get(
Owen Andersonf945a9e2009-07-15 21:51:10 +00001729 std::vector<Constant*>(PTy->getNumElements(), Elt));
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001730 } else {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001731 AllOnes = Constant::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001732 }
1733
1734 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001735 Op->getType(), Name, InsertAtEnd);
1736}
1737
1738
1739// isConstantAllOnes - Helper function for several functions below
1740static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner1edec382007-06-15 06:04:24 +00001741 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
1742 return CI->isAllOnesValue();
1743 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
1744 return CV->isAllOnesValue();
1745 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001746}
1747
Owen Andersonbb2501b2009-07-13 22:18:28 +00001748bool BinaryOperator::isNeg(const Value *V) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001749 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1750 if (Bop->getOpcode() == Instruction::Sub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001751 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
1752 return C->isNegativeZeroValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001753 return false;
1754}
1755
Owen Andersonbb2501b2009-07-13 22:18:28 +00001756bool BinaryOperator::isFNeg(const Value *V) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001757 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1758 if (Bop->getOpcode() == Instruction::FSub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001759 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
Dan Gohman11ff5702009-09-11 00:05:10 +00001760 return C->isNegativeZeroValue();
Dan Gohmana5b96452009-06-04 22:49:04 +00001761 return false;
1762}
1763
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001764bool BinaryOperator::isNot(const Value *V) {
1765 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1766 return (Bop->getOpcode() == Instruction::Xor &&
1767 (isConstantAllOnes(Bop->getOperand(1)) ||
1768 isConstantAllOnes(Bop->getOperand(0))));
1769 return false;
1770}
1771
Chris Lattner2c7d1772005-04-24 07:28:37 +00001772Value *BinaryOperator::getNegArgument(Value *BinOp) {
Chris Lattner2c7d1772005-04-24 07:28:37 +00001773 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001774}
1775
Chris Lattner2c7d1772005-04-24 07:28:37 +00001776const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1777 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001778}
1779
Dan Gohmana5b96452009-06-04 22:49:04 +00001780Value *BinaryOperator::getFNegArgument(Value *BinOp) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001781 return cast<BinaryOperator>(BinOp)->getOperand(1);
1782}
1783
1784const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
1785 return getFNegArgument(const_cast<Value*>(BinOp));
1786}
1787
Chris Lattner2c7d1772005-04-24 07:28:37 +00001788Value *BinaryOperator::getNotArgument(Value *BinOp) {
1789 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1790 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1791 Value *Op0 = BO->getOperand(0);
1792 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001793 if (isConstantAllOnes(Op0)) return Op1;
1794
1795 assert(isConstantAllOnes(Op1));
1796 return Op0;
1797}
1798
Chris Lattner2c7d1772005-04-24 07:28:37 +00001799const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1800 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001801}
1802
1803
1804// swapOperands - Exchange the two operands to this instruction. This
1805// instruction is safe to use on any binary instruction and does not
1806// modify the semantics of the instruction. If the instruction is
1807// order dependent (SetLT f.e.) the opcode is changed.
1808//
1809bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001810 if (!isCommutative())
1811 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00001812 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001813 return false;
1814}
1815
Dan Gohman1b849082009-09-07 23:54:19 +00001816void BinaryOperator::setHasNoUnsignedWrap(bool b) {
1817 cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b);
1818}
1819
1820void BinaryOperator::setHasNoSignedWrap(bool b) {
1821 cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b);
1822}
1823
1824void BinaryOperator::setIsExact(bool b) {
1825 cast<SDivOperator>(this)->setIsExact(b);
1826}
1827
Nick Lewycky28a5f252009-09-27 21:33:04 +00001828bool BinaryOperator::hasNoUnsignedWrap() const {
1829 return cast<OverflowingBinaryOperator>(this)->hasNoUnsignedWrap();
1830}
1831
1832bool BinaryOperator::hasNoSignedWrap() const {
1833 return cast<OverflowingBinaryOperator>(this)->hasNoSignedWrap();
1834}
1835
1836bool BinaryOperator::isExact() const {
1837 return cast<SDivOperator>(this)->isExact();
1838}
1839
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001840//===----------------------------------------------------------------------===//
1841// CastInst Class
1842//===----------------------------------------------------------------------===//
1843
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001844// Just determine if this cast only deals with integral->integral conversion.
1845bool CastInst::isIntegerCast() const {
1846 switch (getOpcode()) {
1847 default: return false;
1848 case Instruction::ZExt:
1849 case Instruction::SExt:
1850 case Instruction::Trunc:
1851 return true;
1852 case Instruction::BitCast:
Duncan Sands9dff9be2010-02-15 16:12:20 +00001853 return getOperand(0)->getType()->isIntegerTy() &&
1854 getType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001855 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001856}
1857
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001858bool CastInst::isLosslessCast() const {
1859 // Only BitCast can be lossless, exit fast if we're not BitCast
1860 if (getOpcode() != Instruction::BitCast)
1861 return false;
1862
1863 // Identity cast is always lossless
1864 const Type* SrcTy = getOperand(0)->getType();
1865 const Type* DstTy = getType();
1866 if (SrcTy == DstTy)
1867 return true;
1868
Reid Spencer8d9336d2006-12-31 05:26:44 +00001869 // Pointer to pointer is always lossless.
Duncan Sands19d0b472010-02-16 11:11:14 +00001870 if (SrcTy->isPointerTy())
1871 return DstTy->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001872 return false; // Other types have no identity values
1873}
1874
1875/// This function determines if the CastInst does not require any bits to be
1876/// changed in order to effect the cast. Essentially, it identifies cases where
1877/// no code gen is necessary for the cast, hence the name no-op cast. For
1878/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00001879/// # bitcast i32* %x to i8*
1880/// # bitcast <2 x i32> %x to <4 x i16>
1881/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Dan Gohman0d7f3b82010-05-28 21:41:37 +00001882/// @brief Determine if the described cast is a no-op.
1883bool CastInst::isNoopCast(Instruction::CastOps Opcode,
1884 const Type *SrcTy,
1885 const Type *DestTy,
1886 const Type *IntPtrTy) {
1887 switch (Opcode) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001888 default:
1889 assert(!"Invalid CastOp");
1890 case Instruction::Trunc:
1891 case Instruction::ZExt:
1892 case Instruction::SExt:
1893 case Instruction::FPTrunc:
1894 case Instruction::FPExt:
1895 case Instruction::UIToFP:
1896 case Instruction::SIToFP:
1897 case Instruction::FPToUI:
1898 case Instruction::FPToSI:
1899 return false; // These always modify bits
1900 case Instruction::BitCast:
1901 return true; // BitCast never modifies bits.
1902 case Instruction::PtrToInt:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001903 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00001904 DestTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001905 case Instruction::IntToPtr:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001906 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00001907 SrcTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001908 }
1909}
1910
Dan Gohman0d7f3b82010-05-28 21:41:37 +00001911/// @brief Determine if a cast is a no-op.
1912bool CastInst::isNoopCast(const Type *IntPtrTy) const {
1913 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
1914}
1915
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001916/// This function determines if a pair of casts can be eliminated and what
1917/// opcode should be used in the elimination. This assumes that there are two
1918/// instructions like this:
1919/// * %F = firstOpcode SrcTy %x to MidTy
1920/// * %S = secondOpcode MidTy %F to DstTy
1921/// The function returns a resultOpcode so these two casts can be replaced with:
1922/// * %Replacement = resultOpcode %SrcTy %x to DstTy
1923/// If no such cast is permited, the function returns 0.
1924unsigned CastInst::isEliminableCastPair(
1925 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
1926 const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
1927{
1928 // Define the 144 possibilities for these two cast instructions. The values
1929 // in this matrix determine what to do in a given situation and select the
1930 // case in the switch below. The rows correspond to firstOp, the columns
1931 // correspond to secondOp. In looking at the table below, keep in mind
1932 // the following cast properties:
1933 //
1934 // Size Compare Source Destination
1935 // Operator Src ? Size Type Sign Type Sign
1936 // -------- ------------ ------------------- ---------------------
1937 // TRUNC > Integer Any Integral Any
1938 // ZEXT < Integral Unsigned Integer Any
1939 // SEXT < Integral Signed Integer Any
1940 // FPTOUI n/a FloatPt n/a Integral Unsigned
1941 // FPTOSI n/a FloatPt n/a Integral Signed
1942 // UITOFP n/a Integral Unsigned FloatPt n/a
1943 // SITOFP n/a Integral Signed FloatPt n/a
1944 // FPTRUNC > FloatPt n/a FloatPt n/a
1945 // FPEXT < FloatPt n/a FloatPt n/a
1946 // PTRTOINT n/a Pointer n/a Integral Unsigned
1947 // INTTOPTR n/a Integral Unsigned Pointer n/a
Dan Gohmaneb7111b2010-04-07 23:22:42 +00001948 // BITCAST = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00001949 //
1950 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00001951 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
1952 // into "fptoui double to i64", but this loses information about the range
Chris Lattner6f6b4972006-12-05 23:43:59 +00001953 // of the produced value (we no longer know the top-part is all zeros).
1954 // Further this conversion is often much more expensive for typical hardware,
1955 // and causes issues when building libgcc. We disallow fptosi+sext for the
1956 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001957 const unsigned numCastOps =
1958 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
1959 static const uint8_t CastResults[numCastOps][numCastOps] = {
1960 // T F F U S F F P I B -+
1961 // R Z S P P I I T P 2 N T |
1962 // U E E 2 2 2 2 R E I T C +- secondOp
1963 // N X X U S F F N X N 2 V |
1964 // C T T I I P P C T T P T -+
1965 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
1966 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
1967 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00001968 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
1969 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001970 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
1971 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
1972 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
1973 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
1974 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
1975 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
1976 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
1977 };
Chris Lattner25eea4d2010-07-12 01:19:22 +00001978
1979 // If either of the casts are a bitcast from scalar to vector, disallow the
1980 // merging.
1981 if ((firstOp == Instruction::BitCast &&
1982 isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) ||
1983 (secondOp == Instruction::BitCast &&
1984 isa<VectorType>(MidTy) != isa<VectorType>(DstTy)))
1985 return 0; // Disallowed
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001986
1987 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
1988 [secondOp-Instruction::CastOpsBegin];
1989 switch (ElimCase) {
1990 case 0:
1991 // categorically disallowed
1992 return 0;
1993 case 1:
1994 // allowed, use first cast's opcode
1995 return firstOp;
1996 case 2:
1997 // allowed, use second cast's opcode
1998 return secondOp;
1999 case 3:
2000 // no-op cast in second op implies firstOp as long as the DestTy
Mon P Wange04b4562010-01-23 04:35:57 +00002001 // is integer and we are not converting between a vector and a
Chris Lattner531732b2010-01-23 04:42:42 +00002002 // non vector type.
Duncan Sands19d0b472010-02-16 11:11:14 +00002003 if (!SrcTy->isVectorTy() && DstTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002004 return firstOp;
2005 return 0;
2006 case 4:
2007 // no-op cast in second op implies firstOp as long as the DestTy
Chris Lattner531732b2010-01-23 04:42:42 +00002008 // is floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002009 if (DstTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002010 return firstOp;
2011 return 0;
2012 case 5:
2013 // no-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002014 // is an integer.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002015 if (SrcTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002016 return secondOp;
2017 return 0;
2018 case 6:
2019 // no-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002020 // is a floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002021 if (SrcTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002022 return secondOp;
2023 return 0;
2024 case 7: {
2025 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
Dan Gohman9413de12009-07-21 23:19:40 +00002026 if (!IntPtrTy)
2027 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002028 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2029 unsigned MidSize = MidTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002030 if (MidSize >= PtrSize)
2031 return Instruction::BitCast;
2032 return 0;
2033 }
2034 case 8: {
2035 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2036 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2037 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002038 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2039 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002040 if (SrcSize == DstSize)
2041 return Instruction::BitCast;
2042 else if (SrcSize < DstSize)
2043 return firstOp;
2044 return secondOp;
2045 }
2046 case 9: // zext, sext -> zext, because sext can't sign extend after zext
2047 return Instruction::ZExt;
2048 case 10:
2049 // fpext followed by ftrunc is allowed if the bit size returned to is
2050 // the same as the original, in which case its just a bitcast
2051 if (SrcTy == DstTy)
2052 return Instruction::BitCast;
2053 return 0; // If the types are not the same we can't eliminate it.
2054 case 11:
2055 // bitcast followed by ptrtoint is allowed as long as the bitcast
2056 // is a pointer to pointer cast.
Duncan Sands19d0b472010-02-16 11:11:14 +00002057 if (SrcTy->isPointerTy() && MidTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002058 return secondOp;
2059 return 0;
2060 case 12:
2061 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
Duncan Sands19d0b472010-02-16 11:11:14 +00002062 if (MidTy->isPointerTy() && DstTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002063 return firstOp;
2064 return 0;
2065 case 13: {
2066 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Dan Gohman9413de12009-07-21 23:19:40 +00002067 if (!IntPtrTy)
2068 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002069 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2070 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2071 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002072 if (SrcSize <= PtrSize && SrcSize == DstSize)
2073 return Instruction::BitCast;
2074 return 0;
2075 }
2076 case 99:
2077 // cast combination can't happen (error in input). This is for all cases
2078 // where the MidTy is not the same for the two cast instructions.
2079 assert(!"Invalid Cast Combination");
2080 return 0;
2081 default:
2082 assert(!"Error in CastResults table!!!");
2083 return 0;
2084 }
2085 return 0;
2086}
2087
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002088CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002089 const Twine &Name, Instruction *InsertBefore) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002090 // Construct and return the appropriate CastInst subclass
2091 switch (op) {
2092 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2093 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2094 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2095 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2096 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2097 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2098 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2099 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2100 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2101 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2102 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2103 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
2104 default:
2105 assert(!"Invalid opcode provided");
2106 }
2107 return 0;
2108}
2109
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002110CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002111 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002112 // Construct and return the appropriate CastInst subclass
2113 switch (op) {
2114 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2115 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2116 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2117 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2118 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2119 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2120 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2121 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2122 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2123 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2124 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2125 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
2126 default:
2127 assert(!"Invalid opcode provided");
2128 }
2129 return 0;
2130}
2131
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002132CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002133 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002134 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002135 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002136 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2137 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002138}
2139
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002140CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002141 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002142 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002143 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002144 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2145 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002146}
2147
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002148CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002149 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002150 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002151 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002152 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2153 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002154}
2155
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002156CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002157 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002158 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002159 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002160 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2161 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002162}
2163
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002164CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002165 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002166 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002167 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002168 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2169 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002170}
2171
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002172CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002173 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002174 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002175 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002176 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2177 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002178}
2179
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002180CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002181 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002182 BasicBlock *InsertAtEnd) {
Duncan Sands19d0b472010-02-16 11:11:14 +00002183 assert(S->getType()->isPointerTy() && "Invalid cast");
2184 assert((Ty->isIntegerTy() || Ty->isPointerTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002185 "Invalid cast");
2186
Duncan Sands9dff9be2010-02-15 16:12:20 +00002187 if (Ty->isIntegerTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002188 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
2189 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002190}
2191
2192/// @brief Create a BitCast or a PtrToInt cast instruction
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002193CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002194 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002195 Instruction *InsertBefore) {
Duncan Sands19d0b472010-02-16 11:11:14 +00002196 assert(S->getType()->isPointerTy() && "Invalid cast");
2197 assert((Ty->isIntegerTy() || Ty->isPointerTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002198 "Invalid cast");
2199
Duncan Sands9dff9be2010-02-15 16:12:20 +00002200 if (Ty->isIntegerTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002201 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2202 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002203}
2204
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002205CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002206 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002207 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002208 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Chris Lattner5370ae72010-01-10 20:21:42 +00002209 "Invalid integer cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002210 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2211 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002212 Instruction::CastOps opcode =
2213 (SrcBits == DstBits ? Instruction::BitCast :
2214 (SrcBits > DstBits ? Instruction::Trunc :
2215 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002216 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002217}
2218
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002219CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002220 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002221 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002222 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002223 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002224 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2225 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002226 Instruction::CastOps opcode =
2227 (SrcBits == DstBits ? Instruction::BitCast :
2228 (SrcBits > DstBits ? Instruction::Trunc :
2229 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002230 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002231}
2232
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002233CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002234 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002235 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002236 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002237 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002238 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2239 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002240 Instruction::CastOps opcode =
2241 (SrcBits == DstBits ? Instruction::BitCast :
2242 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002243 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002244}
2245
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002246CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002247 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002248 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002249 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002250 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002251 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2252 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002253 Instruction::CastOps opcode =
2254 (SrcBits == DstBits ? Instruction::BitCast :
2255 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002256 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002257}
2258
Duncan Sands55e50902008-01-06 10:12:28 +00002259// Check whether it is valid to call getCastOpcode for these types.
2260// This routine must be kept in sync with getCastOpcode.
2261bool CastInst::isCastable(const Type *SrcTy, const Type *DestTy) {
2262 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2263 return false;
2264
2265 if (SrcTy == DestTy)
2266 return true;
2267
2268 // Get the bit sizes, we'll need these
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002269 unsigned SrcBits = SrcTy->getScalarSizeInBits(); // 0 for ptr
2270 unsigned DestBits = DestTy->getScalarSizeInBits(); // 0 for ptr
Duncan Sands55e50902008-01-06 10:12:28 +00002271
2272 // Run through the possibilities ...
Duncan Sands9dff9be2010-02-15 16:12:20 +00002273 if (DestTy->isIntegerTy()) { // Casting to integral
2274 if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002275 return true;
Duncan Sands9dff9be2010-02-15 16:12:20 +00002276 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002277 return true;
2278 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002279 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002280 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002281 } else { // Casting from something else
Duncan Sands19d0b472010-02-16 11:11:14 +00002282 return SrcTy->isPointerTy();
Duncan Sands55e50902008-01-06 10:12:28 +00002283 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002284 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2285 if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002286 return true;
Duncan Sands9dff9be2010-02-15 16:12:20 +00002287 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002288 return true;
2289 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002290 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002291 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002292 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002293 return false;
2294 }
2295 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002296 // Casting to vector
Duncan Sands55e50902008-01-06 10:12:28 +00002297 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002298 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002299 return DestPTy->getBitWidth() == SrcPTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002300 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002301 return DestPTy->getBitWidth() == SrcBits;
2302 }
Duncan Sands19d0b472010-02-16 11:11:14 +00002303 } else if (DestTy->isPointerTy()) { // Casting to pointer
2304 if (SrcTy->isPointerTy()) { // Casting from pointer
Duncan Sands55e50902008-01-06 10:12:28 +00002305 return true;
Duncan Sands9dff9be2010-02-15 16:12:20 +00002306 } else if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002307 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002308 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002309 return false;
2310 }
Dale Johannesendd224d22010-09-30 23:57:10 +00002311 } else if (DestTy->isX86_MMXTy()) {
2312 return SrcBits == 64;
Gabor Greif697e94c2008-05-15 10:04:30 +00002313 } else { // Casting to something else
Duncan Sands55e50902008-01-06 10:12:28 +00002314 return false;
2315 }
2316}
2317
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002318// Provide a way to get a "cast" where the cast opcode is inferred from the
2319// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002320// logic in the castIsValid function below. This axiom should hold:
2321// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2322// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002323// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002324// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002325Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002326CastInst::getCastOpcode(
2327 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002328 // Get the bit sizes, we'll need these
2329 const Type *SrcTy = Src->getType();
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002330 unsigned SrcBits = SrcTy->getScalarSizeInBits(); // 0 for ptr
2331 unsigned DestBits = DestTy->getScalarSizeInBits(); // 0 for ptr
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002332
Duncan Sands55e50902008-01-06 10:12:28 +00002333 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2334 "Only first class types are castable!");
2335
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002336 // Run through the possibilities ...
Duncan Sands9dff9be2010-02-15 16:12:20 +00002337 if (DestTy->isIntegerTy()) { // Casting to integral
2338 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002339 if (DestBits < SrcBits)
2340 return Trunc; // int -> smaller int
2341 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002342 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002343 return SExt; // signed -> SEXT
2344 else
2345 return ZExt; // unsigned -> ZEXT
2346 } else {
2347 return BitCast; // Same size, No-op cast
2348 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002349 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002350 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002351 return FPToSI; // FP -> sint
2352 else
2353 return FPToUI; // FP -> uint
Reid Spencerd84d35b2007-02-15 02:26:10 +00002354 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002355 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002356 "Casting vector to integer of different width");
Devang Patele9432132008-11-05 01:37:40 +00002357 PTy = NULL;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002358 return BitCast; // Same size, no-op cast
2359 } else {
Duncan Sands19d0b472010-02-16 11:11:14 +00002360 assert(SrcTy->isPointerTy() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002361 "Casting from a value that is not first-class type");
2362 return PtrToInt; // ptr -> int
2363 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002364 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2365 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002366 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002367 return SIToFP; // sint -> FP
2368 else
2369 return UIToFP; // uint -> FP
Duncan Sands9dff9be2010-02-15 16:12:20 +00002370 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002371 if (DestBits < SrcBits) {
2372 return FPTrunc; // FP -> smaller FP
2373 } else if (DestBits > SrcBits) {
2374 return FPExt; // FP -> larger FP
2375 } else {
2376 return BitCast; // same size, no-op cast
2377 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002378 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002379 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002380 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00002381 PTy = NULL;
2382 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002383 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002384 llvm_unreachable("Casting pointer or non-first class to float");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002385 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002386 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
2387 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002388 assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002389 "Casting vector to vector of different widths");
Devang Patelcb181bb2008-11-21 20:00:59 +00002390 SrcPTy = NULL;
Dan Gohmanfead7972007-05-11 21:43:24 +00002391 return BitCast; // vector -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002392 } else if (DestPTy->getBitWidth() == SrcBits) {
Dan Gohmanfead7972007-05-11 21:43:24 +00002393 return BitCast; // float/int -> vector
Dale Johannesendd224d22010-09-30 23:57:10 +00002394 } else if (SrcTy->isX86_MMXTy()) {
2395 assert(DestPTy->getBitWidth()==64 &&
2396 "Casting X86_MMX to vector of wrong width");
2397 return BitCast; // MMX to 64-bit vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002398 } else {
Dan Gohmanfead7972007-05-11 21:43:24 +00002399 assert(!"Illegal cast to vector (wrong type or size)");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002400 }
Duncan Sands19d0b472010-02-16 11:11:14 +00002401 } else if (DestTy->isPointerTy()) {
2402 if (SrcTy->isPointerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002403 return BitCast; // ptr -> ptr
Duncan Sands9dff9be2010-02-15 16:12:20 +00002404 } else if (SrcTy->isIntegerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002405 return IntToPtr; // int -> ptr
2406 } else {
2407 assert(!"Casting pointer to other than pointer or int");
2408 }
Dale Johannesendd224d22010-09-30 23:57:10 +00002409 } else if (DestTy->isX86_MMXTy()) {
Bill Wendling3c704172010-10-03 00:46:06 +00002410 if (isa<VectorType>(SrcTy)) {
2411 assert(cast<VectorType>(SrcTy)->getBitWidth() == 64 &&
Dale Johannesendd224d22010-09-30 23:57:10 +00002412 "Casting vector of wrong width to X86_MMX");
2413 return BitCast; // 64-bit vector to MMX
2414 } else {
2415 assert(!"Illegal cast to X86_MMX");
2416 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002417 } else {
2418 assert(!"Casting to type that is not first-class");
2419 }
2420
2421 // If we fall through to here we probably hit an assertion cast above
2422 // and assertions are not turned on. Anything we return is an error, so
2423 // BitCast is as good a choice as any.
2424 return BitCast;
2425}
2426
2427//===----------------------------------------------------------------------===//
2428// CastInst SubClass Constructors
2429//===----------------------------------------------------------------------===//
2430
2431/// Check that the construction parameters for a CastInst are correct. This
2432/// could be broken out into the separate constructors but it is useful to have
2433/// it in one place and to eliminate the redundant code for getting the sizes
2434/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002435bool
2436CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002437
2438 // Check for type sanity on the arguments
2439 const Type *SrcTy = S->getType();
Chris Lattner37bc78a2010-01-26 21:51:43 +00002440 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() ||
2441 SrcTy->isAggregateType() || DstTy->isAggregateType())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002442 return false;
2443
2444 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002445 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2446 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002447
2448 // Switch on the opcode provided
2449 switch (op) {
2450 default: return false; // This is an input error
2451 case Instruction::Trunc:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002452 return SrcTy->isIntOrIntVectorTy() &&
2453 DstTy->isIntOrIntVectorTy()&& SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002454 case Instruction::ZExt:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002455 return SrcTy->isIntOrIntVectorTy() &&
2456 DstTy->isIntOrIntVectorTy()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002457 case Instruction::SExt:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002458 return SrcTy->isIntOrIntVectorTy() &&
2459 DstTy->isIntOrIntVectorTy()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002460 case Instruction::FPTrunc:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002461 return SrcTy->isFPOrFPVectorTy() &&
2462 DstTy->isFPOrFPVectorTy() &&
Dan Gohman550c9af2008-08-14 20:04:46 +00002463 SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002464 case Instruction::FPExt:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002465 return SrcTy->isFPOrFPVectorTy() &&
2466 DstTy->isFPOrFPVectorTy() &&
Dan Gohman550c9af2008-08-14 20:04:46 +00002467 SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002468 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002469 case Instruction::SIToFP:
Nate Begemand4d45c22007-11-17 03:58:34 +00002470 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2471 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002472 return SVTy->getElementType()->isIntOrIntVectorTy() &&
2473 DVTy->getElementType()->isFPOrFPVectorTy() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00002474 SVTy->getNumElements() == DVTy->getNumElements();
2475 }
2476 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002477 return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002478 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002479 case Instruction::FPToSI:
Nate Begemand4d45c22007-11-17 03:58:34 +00002480 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2481 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002482 return SVTy->getElementType()->isFPOrFPVectorTy() &&
2483 DVTy->getElementType()->isIntOrIntVectorTy() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00002484 SVTy->getNumElements() == DVTy->getNumElements();
2485 }
2486 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002487 return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002488 case Instruction::PtrToInt:
Duncan Sands19d0b472010-02-16 11:11:14 +00002489 return SrcTy->isPointerTy() && DstTy->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002490 case Instruction::IntToPtr:
Duncan Sands19d0b472010-02-16 11:11:14 +00002491 return SrcTy->isIntegerTy() && DstTy->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002492 case Instruction::BitCast:
2493 // BitCast implies a no-op cast of type only. No bits change.
2494 // However, you can't cast pointers to anything but pointers.
Duncan Sands19d0b472010-02-16 11:11:14 +00002495 if (SrcTy->isPointerTy() != DstTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002496 return false;
2497
Duncan Sands55e50902008-01-06 10:12:28 +00002498 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002499 // these cases, the cast is okay if the source and destination bit widths
2500 // are identical.
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002501 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002502 }
2503}
2504
2505TruncInst::TruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002506 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002507) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002508 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002509}
2510
2511TruncInst::TruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002512 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002513) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002514 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002515}
2516
2517ZExtInst::ZExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002518 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002519) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002520 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002521}
2522
2523ZExtInst::ZExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002524 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002525) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002526 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002527}
2528SExtInst::SExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002529 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002530) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002531 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002532}
2533
Jeff Cohencc08c832006-12-02 02:22:01 +00002534SExtInst::SExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002535 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002536) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002537 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002538}
2539
2540FPTruncInst::FPTruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002541 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002542) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002543 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002544}
2545
2546FPTruncInst::FPTruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002547 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002548) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002549 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002550}
2551
2552FPExtInst::FPExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002553 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002554) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002555 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002556}
2557
2558FPExtInst::FPExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002559 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002560) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002561 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002562}
2563
2564UIToFPInst::UIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002565 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002566) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002567 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002568}
2569
2570UIToFPInst::UIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002571 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002572) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002573 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002574}
2575
2576SIToFPInst::SIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002577 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002578) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002579 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002580}
2581
2582SIToFPInst::SIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002583 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002584) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002585 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002586}
2587
2588FPToUIInst::FPToUIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002589 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002590) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002591 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002592}
2593
2594FPToUIInst::FPToUIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002595 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002596) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002597 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002598}
2599
2600FPToSIInst::FPToSIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002601 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002602) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002603 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002604}
2605
2606FPToSIInst::FPToSIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002607 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002608) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002609 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002610}
2611
2612PtrToIntInst::PtrToIntInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002613 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002614) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002615 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002616}
2617
2618PtrToIntInst::PtrToIntInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002619 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002620) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002621 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002622}
2623
2624IntToPtrInst::IntToPtrInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002625 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002626) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002627 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002628}
2629
2630IntToPtrInst::IntToPtrInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002631 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002632) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002633 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002634}
2635
2636BitCastInst::BitCastInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002637 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002638) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002639 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002640}
2641
2642BitCastInst::BitCastInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002643 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002644) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002645 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002646}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002647
2648//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002649// CmpInst Classes
2650//===----------------------------------------------------------------------===//
2651
Chris Lattneraec33da2010-01-22 06:25:37 +00002652void CmpInst::Anchor() const {}
2653
Nate Begemand2195702008-05-12 19:01:56 +00002654CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002655 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002656 Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002657 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002658 OperandTraits<CmpInst>::op_begin(this),
2659 OperandTraits<CmpInst>::operands(this),
2660 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002661 Op<0>() = LHS;
2662 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00002663 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00002664 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002665}
Gabor Greiff6caff662008-05-10 08:32:32 +00002666
Nate Begemand2195702008-05-12 19:01:56 +00002667CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002668 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002669 BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002670 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002671 OperandTraits<CmpInst>::op_begin(this),
2672 OperandTraits<CmpInst>::operands(this),
2673 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002674 Op<0>() = LHS;
2675 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00002676 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00002677 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002678}
2679
2680CmpInst *
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002681CmpInst::Create(OtherOps Op, unsigned short predicate,
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002682 Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002683 const Twine &Name, Instruction *InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002684 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002685 if (InsertBefore)
2686 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
2687 S1, S2, Name);
2688 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002689 return new ICmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002690 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002691 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002692
2693 if (InsertBefore)
2694 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
2695 S1, S2, Name);
2696 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002697 return new FCmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002698 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002699}
2700
2701CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002702CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002703 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002704 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002705 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2706 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002707 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002708 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2709 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002710}
2711
2712void CmpInst::swapOperands() {
2713 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2714 IC->swapOperands();
2715 else
2716 cast<FCmpInst>(this)->swapOperands();
2717}
2718
Duncan Sands95c4ecc2011-01-04 12:52:29 +00002719bool CmpInst::isCommutative() const {
2720 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00002721 return IC->isCommutative();
2722 return cast<FCmpInst>(this)->isCommutative();
2723}
2724
Duncan Sands95c4ecc2011-01-04 12:52:29 +00002725bool CmpInst::isEquality() const {
2726 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00002727 return IC->isEquality();
2728 return cast<FCmpInst>(this)->isEquality();
2729}
2730
2731
Dan Gohman4e724382008-05-31 02:47:54 +00002732CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002733 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002734 default: assert(!"Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002735 case ICMP_EQ: return ICMP_NE;
2736 case ICMP_NE: return ICMP_EQ;
2737 case ICMP_UGT: return ICMP_ULE;
2738 case ICMP_ULT: return ICMP_UGE;
2739 case ICMP_UGE: return ICMP_ULT;
2740 case ICMP_ULE: return ICMP_UGT;
2741 case ICMP_SGT: return ICMP_SLE;
2742 case ICMP_SLT: return ICMP_SGE;
2743 case ICMP_SGE: return ICMP_SLT;
2744 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00002745
Dan Gohman4e724382008-05-31 02:47:54 +00002746 case FCMP_OEQ: return FCMP_UNE;
2747 case FCMP_ONE: return FCMP_UEQ;
2748 case FCMP_OGT: return FCMP_ULE;
2749 case FCMP_OLT: return FCMP_UGE;
2750 case FCMP_OGE: return FCMP_ULT;
2751 case FCMP_OLE: return FCMP_UGT;
2752 case FCMP_UEQ: return FCMP_ONE;
2753 case FCMP_UNE: return FCMP_OEQ;
2754 case FCMP_UGT: return FCMP_OLE;
2755 case FCMP_ULT: return FCMP_OGE;
2756 case FCMP_UGE: return FCMP_OLT;
2757 case FCMP_ULE: return FCMP_OGT;
2758 case FCMP_ORD: return FCMP_UNO;
2759 case FCMP_UNO: return FCMP_ORD;
2760 case FCMP_TRUE: return FCMP_FALSE;
2761 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00002762 }
2763}
2764
Reid Spencer266e42b2006-12-23 06:05:41 +00002765ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2766 switch (pred) {
2767 default: assert(! "Unknown icmp predicate!");
2768 case ICMP_EQ: case ICMP_NE:
2769 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2770 return pred;
2771 case ICMP_UGT: return ICMP_SGT;
2772 case ICMP_ULT: return ICMP_SLT;
2773 case ICMP_UGE: return ICMP_SGE;
2774 case ICMP_ULE: return ICMP_SLE;
2775 }
2776}
2777
Nick Lewycky8ea81e82008-01-28 03:48:02 +00002778ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
2779 switch (pred) {
2780 default: assert(! "Unknown icmp predicate!");
2781 case ICMP_EQ: case ICMP_NE:
2782 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
2783 return pred;
2784 case ICMP_SGT: return ICMP_UGT;
2785 case ICMP_SLT: return ICMP_ULT;
2786 case ICMP_SGE: return ICMP_UGE;
2787 case ICMP_SLE: return ICMP_ULE;
2788 }
2789}
2790
Reid Spencer0286bc12007-02-28 22:00:54 +00002791/// Initialize a set of values that all satisfy the condition with C.
2792///
2793ConstantRange
2794ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2795 APInt Lower(C);
2796 APInt Upper(C);
2797 uint32_t BitWidth = C.getBitWidth();
2798 switch (pred) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002799 default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!");
Reid Spencer0286bc12007-02-28 22:00:54 +00002800 case ICmpInst::ICMP_EQ: Upper++; break;
2801 case ICmpInst::ICMP_NE: Lower++; break;
Dan Gohmand86e2952010-01-26 16:04:20 +00002802 case ICmpInst::ICMP_ULT:
2803 Lower = APInt::getMinValue(BitWidth);
2804 // Check for an empty-set condition.
2805 if (Lower == Upper)
2806 return ConstantRange(BitWidth, /*isFullSet=*/false);
2807 break;
2808 case ICmpInst::ICMP_SLT:
2809 Lower = APInt::getSignedMinValue(BitWidth);
2810 // Check for an empty-set condition.
2811 if (Lower == Upper)
2812 return ConstantRange(BitWidth, /*isFullSet=*/false);
2813 break;
Reid Spencer0286bc12007-02-28 22:00:54 +00002814 case ICmpInst::ICMP_UGT:
2815 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00002816 // Check for an empty-set condition.
2817 if (Lower == Upper)
2818 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00002819 break;
2820 case ICmpInst::ICMP_SGT:
2821 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00002822 // Check for an empty-set condition.
2823 if (Lower == Upper)
2824 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00002825 break;
2826 case ICmpInst::ICMP_ULE:
2827 Lower = APInt::getMinValue(BitWidth); Upper++;
Dan Gohmand86e2952010-01-26 16:04:20 +00002828 // Check for a full-set condition.
2829 if (Lower == Upper)
2830 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00002831 break;
2832 case ICmpInst::ICMP_SLE:
2833 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
Dan Gohmand86e2952010-01-26 16:04:20 +00002834 // Check for a full-set condition.
2835 if (Lower == Upper)
2836 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00002837 break;
2838 case ICmpInst::ICMP_UGE:
2839 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00002840 // Check for a full-set condition.
2841 if (Lower == Upper)
2842 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00002843 break;
2844 case ICmpInst::ICMP_SGE:
2845 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00002846 // Check for a full-set condition.
2847 if (Lower == Upper)
2848 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00002849 break;
2850 }
2851 return ConstantRange(Lower, Upper);
2852}
2853
Dan Gohman4e724382008-05-31 02:47:54 +00002854CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002855 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002856 default: assert(!"Unknown cmp predicate!");
2857 case ICMP_EQ: case ICMP_NE:
2858 return pred;
2859 case ICMP_SGT: return ICMP_SLT;
2860 case ICMP_SLT: return ICMP_SGT;
2861 case ICMP_SGE: return ICMP_SLE;
2862 case ICMP_SLE: return ICMP_SGE;
2863 case ICMP_UGT: return ICMP_ULT;
2864 case ICMP_ULT: return ICMP_UGT;
2865 case ICMP_UGE: return ICMP_ULE;
2866 case ICMP_ULE: return ICMP_UGE;
2867
Reid Spencerd9436b62006-11-20 01:22:35 +00002868 case FCMP_FALSE: case FCMP_TRUE:
2869 case FCMP_OEQ: case FCMP_ONE:
2870 case FCMP_UEQ: case FCMP_UNE:
2871 case FCMP_ORD: case FCMP_UNO:
2872 return pred;
2873 case FCMP_OGT: return FCMP_OLT;
2874 case FCMP_OLT: return FCMP_OGT;
2875 case FCMP_OGE: return FCMP_OLE;
2876 case FCMP_OLE: return FCMP_OGE;
2877 case FCMP_UGT: return FCMP_ULT;
2878 case FCMP_ULT: return FCMP_UGT;
2879 case FCMP_UGE: return FCMP_ULE;
2880 case FCMP_ULE: return FCMP_UGE;
2881 }
2882}
2883
Reid Spencer266e42b2006-12-23 06:05:41 +00002884bool CmpInst::isUnsigned(unsigned short predicate) {
2885 switch (predicate) {
2886 default: return false;
2887 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2888 case ICmpInst::ICMP_UGE: return true;
2889 }
2890}
2891
Nick Lewycky7494b3b2009-10-25 03:50:03 +00002892bool CmpInst::isSigned(unsigned short predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002893 switch (predicate) {
2894 default: return false;
2895 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2896 case ICmpInst::ICMP_SGE: return true;
2897 }
2898}
2899
2900bool CmpInst::isOrdered(unsigned short predicate) {
2901 switch (predicate) {
2902 default: return false;
2903 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2904 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2905 case FCmpInst::FCMP_ORD: return true;
2906 }
2907}
2908
2909bool CmpInst::isUnordered(unsigned short predicate) {
2910 switch (predicate) {
2911 default: return false;
2912 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2913 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2914 case FCmpInst::FCMP_UNO: return true;
2915 }
2916}
2917
Nick Lewycky7494b3b2009-10-25 03:50:03 +00002918bool CmpInst::isTrueWhenEqual(unsigned short predicate) {
2919 switch(predicate) {
2920 default: return false;
2921 case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE:
2922 case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true;
2923 }
2924}
2925
2926bool CmpInst::isFalseWhenEqual(unsigned short predicate) {
2927 switch(predicate) {
2928 case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT:
2929 case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true;
2930 default: return false;
2931 }
2932}
2933
2934
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002935//===----------------------------------------------------------------------===//
2936// SwitchInst Implementation
2937//===----------------------------------------------------------------------===//
2938
Chris Lattnerbaf00152010-11-17 05:41:46 +00002939void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumReserved) {
2940 assert(Value && Default && NumReserved);
2941 ReservedSpace = NumReserved;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002942 NumOperands = 2;
Gabor Greiff6caff662008-05-10 08:32:32 +00002943 OperandList = allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002944
Gabor Greif2d3024d2008-05-26 21:33:52 +00002945 OperandList[0] = Value;
2946 OperandList[1] = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002947}
2948
Chris Lattner2195fc42007-02-24 00:55:48 +00002949/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2950/// switch on and a default destination. The number of additional cases can
2951/// be specified here to make memory allocation more efficient. This
2952/// constructor can also autoinsert before another instruction.
2953SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2954 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00002955 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
2956 0, 0, InsertBefore) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00002957 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00002958}
2959
2960/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2961/// switch on and a default destination. The number of additional cases can
2962/// be specified here to make memory allocation more efficient. This
2963/// constructor also autoinserts at the end of the specified BasicBlock.
2964SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2965 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00002966 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
2967 0, 0, InsertAtEnd) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00002968 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00002969}
2970
Misha Brukmanb1c93172005-04-21 23:48:37 +00002971SwitchInst::SwitchInst(const SwitchInst &SI)
Chris Lattnerbaf00152010-11-17 05:41:46 +00002972 : TerminatorInst(SI.getType(), Instruction::Switch, 0, 0) {
2973 init(SI.getCondition(), SI.getDefaultDest(), SI.getNumOperands());
2974 NumOperands = SI.getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002975 Use *OL = OperandList, *InOL = SI.OperandList;
Chris Lattnerbaf00152010-11-17 05:41:46 +00002976 for (unsigned i = 2, E = SI.getNumOperands(); i != E; i += 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002977 OL[i] = InOL[i];
2978 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002979 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002980 SubclassOptionalData = SI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002981}
2982
Gordon Henriksen14a55692007-12-10 02:14:30 +00002983SwitchInst::~SwitchInst() {
Jay Foadbbb91f22011-01-16 15:30:52 +00002984 dropHungoffUses();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002985}
2986
2987
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002988/// addCase - Add an entry to the switch instruction...
2989///
Chris Lattner47ac1872005-02-24 05:32:09 +00002990void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002991 unsigned OpNo = NumOperands;
2992 if (OpNo+2 > ReservedSpace)
2993 resizeOperands(0); // Get more space!
2994 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002995 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002996 NumOperands = OpNo+2;
Gabor Greif2d3024d2008-05-26 21:33:52 +00002997 OperandList[OpNo] = OnVal;
2998 OperandList[OpNo+1] = Dest;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002999}
3000
3001/// removeCase - This method removes the specified successor from the switch
3002/// instruction. Note that this cannot be used to remove the default
3003/// destination (successor #0).
3004///
3005void SwitchInst::removeCase(unsigned idx) {
3006 assert(idx != 0 && "Cannot remove the default case!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003007 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
3008
3009 unsigned NumOps = getNumOperands();
3010 Use *OL = OperandList;
3011
Jay Foad14277722011-02-01 09:22:34 +00003012 // Overwrite this case with the end of the list.
3013 if ((idx + 1) * 2 != NumOps) {
3014 OL[idx * 2] = OL[NumOps - 2];
3015 OL[idx * 2 + 1] = OL[NumOps - 1];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003016 }
3017
3018 // Nuke the last value.
3019 OL[NumOps-2].set(0);
3020 OL[NumOps-2+1].set(0);
3021 NumOperands = NumOps-2;
3022}
3023
3024/// resizeOperands - resize operands - This adjusts the length of the operands
3025/// list according to the following behavior:
3026/// 1. If NumOps == 0, grow the operand list in response to a push_back style
Gabor Greiff6caff662008-05-10 08:32:32 +00003027/// of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003028/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
3029/// 3. If NumOps == NumOperands, trim the reserved space.
3030///
3031void SwitchInst::resizeOperands(unsigned NumOps) {
Gabor Greiff6caff662008-05-10 08:32:32 +00003032 unsigned e = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003033 if (NumOps == 0) {
Gabor Greiff6caff662008-05-10 08:32:32 +00003034 NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003035 } else if (NumOps*2 > NumOperands) {
3036 // No resize needed.
3037 if (ReservedSpace >= NumOps) return;
3038 } else if (NumOps == NumOperands) {
3039 if (ReservedSpace == NumOps) return;
3040 } else {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00003041 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003042 }
3043
3044 ReservedSpace = NumOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00003045 Use *NewOps = allocHungoffUses(NumOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003046 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +00003047 for (unsigned i = 0; i != e; ++i) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003048 NewOps[i] = OldOps[i];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003049 }
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003050 OperandList = NewOps;
Jay Foadbbb91f22011-01-16 15:30:52 +00003051 Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003052}
3053
3054
3055BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
3056 return getSuccessor(idx);
3057}
3058unsigned SwitchInst::getNumSuccessorsV() const {
3059 return getNumSuccessors();
3060}
3061void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
3062 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003063}
Chris Lattnerf22be932004-10-15 23:52:53 +00003064
Chris Lattner3ed871f2009-10-27 19:13:16 +00003065//===----------------------------------------------------------------------===//
Jay Foadbbb91f22011-01-16 15:30:52 +00003066// IndirectBrInst Implementation
Chris Lattner3ed871f2009-10-27 19:13:16 +00003067//===----------------------------------------------------------------------===//
3068
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003069void IndirectBrInst::init(Value *Address, unsigned NumDests) {
Duncan Sands19d0b472010-02-16 11:11:14 +00003070 assert(Address && Address->getType()->isPointerTy() &&
Chris Lattner6747b4c2009-10-29 05:53:32 +00003071 "Address of indirectbr must be a pointer");
Chris Lattner3ed871f2009-10-27 19:13:16 +00003072 ReservedSpace = 1+NumDests;
3073 NumOperands = 1;
3074 OperandList = allocHungoffUses(ReservedSpace);
3075
3076 OperandList[0] = Address;
3077}
3078
3079
3080/// resizeOperands - resize operands - This adjusts the length of the operands
3081/// list according to the following behavior:
3082/// 1. If NumOps == 0, grow the operand list in response to a push_back style
3083/// of operation. This grows the number of ops by 2 times.
3084/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
3085/// 3. If NumOps == NumOperands, trim the reserved space.
3086///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003087void IndirectBrInst::resizeOperands(unsigned NumOps) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003088 unsigned e = getNumOperands();
3089 if (NumOps == 0) {
3090 NumOps = e*2;
3091 } else if (NumOps*2 > NumOperands) {
3092 // No resize needed.
3093 if (ReservedSpace >= NumOps) return;
3094 } else if (NumOps == NumOperands) {
3095 if (ReservedSpace == NumOps) return;
3096 } else {
3097 return;
3098 }
3099
3100 ReservedSpace = NumOps;
3101 Use *NewOps = allocHungoffUses(NumOps);
3102 Use *OldOps = OperandList;
3103 for (unsigned i = 0; i != e; ++i)
3104 NewOps[i] = OldOps[i];
3105 OperandList = NewOps;
Jay Foadbbb91f22011-01-16 15:30:52 +00003106 Use::zap(OldOps, OldOps + e, true);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003107}
3108
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003109IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3110 Instruction *InsertBefore)
3111: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003112 0, 0, InsertBefore) {
3113 init(Address, NumCases);
3114}
3115
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003116IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3117 BasicBlock *InsertAtEnd)
3118: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003119 0, 0, InsertAtEnd) {
3120 init(Address, NumCases);
3121}
3122
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003123IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI)
3124 : TerminatorInst(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003125 allocHungoffUses(IBI.getNumOperands()),
3126 IBI.getNumOperands()) {
3127 Use *OL = OperandList, *InOL = IBI.OperandList;
3128 for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i)
3129 OL[i] = InOL[i];
3130 SubclassOptionalData = IBI.SubclassOptionalData;
3131}
3132
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003133IndirectBrInst::~IndirectBrInst() {
Jay Foadbbb91f22011-01-16 15:30:52 +00003134 dropHungoffUses();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003135}
3136
3137/// addDestination - Add a destination.
3138///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003139void IndirectBrInst::addDestination(BasicBlock *DestBB) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003140 unsigned OpNo = NumOperands;
3141 if (OpNo+1 > ReservedSpace)
3142 resizeOperands(0); // Get more space!
3143 // Initialize some new operands.
3144 assert(OpNo < ReservedSpace && "Growing didn't work!");
3145 NumOperands = OpNo+1;
3146 OperandList[OpNo] = DestBB;
3147}
3148
3149/// removeDestination - This method removes the specified successor from the
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003150/// indirectbr instruction.
3151void IndirectBrInst::removeDestination(unsigned idx) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003152 assert(idx < getNumOperands()-1 && "Successor index out of range!");
3153
3154 unsigned NumOps = getNumOperands();
3155 Use *OL = OperandList;
3156
3157 // Replace this value with the last one.
3158 OL[idx+1] = OL[NumOps-1];
3159
3160 // Nuke the last value.
3161 OL[NumOps-1].set(0);
3162 NumOperands = NumOps-1;
3163}
3164
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003165BasicBlock *IndirectBrInst::getSuccessorV(unsigned idx) const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003166 return getSuccessor(idx);
3167}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003168unsigned IndirectBrInst::getNumSuccessorsV() const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003169 return getNumSuccessors();
3170}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003171void IndirectBrInst::setSuccessorV(unsigned idx, BasicBlock *B) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003172 setSuccessor(idx, B);
3173}
3174
3175//===----------------------------------------------------------------------===//
Devang Patel11cf3f42009-10-27 22:16:29 +00003176// clone_impl() implementations
Chris Lattner3ed871f2009-10-27 19:13:16 +00003177//===----------------------------------------------------------------------===//
3178
Chris Lattnerf22be932004-10-15 23:52:53 +00003179// Define these methods here so vtables don't get emitted into every translation
3180// unit that uses these classes.
3181
Devang Patel11cf3f42009-10-27 22:16:29 +00003182GetElementPtrInst *GetElementPtrInst::clone_impl() const {
3183 return new (getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00003184}
3185
Devang Patel11cf3f42009-10-27 22:16:29 +00003186BinaryOperator *BinaryOperator::clone_impl() const {
3187 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00003188}
3189
Devang Patel11cf3f42009-10-27 22:16:29 +00003190FCmpInst* FCmpInst::clone_impl() const {
3191 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00003192}
3193
Devang Patel11cf3f42009-10-27 22:16:29 +00003194ICmpInst* ICmpInst::clone_impl() const {
3195 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohman0752bff2008-05-23 00:36:11 +00003196}
3197
Devang Patel11cf3f42009-10-27 22:16:29 +00003198ExtractValueInst *ExtractValueInst::clone_impl() const {
3199 return new ExtractValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003200}
3201
Devang Patel11cf3f42009-10-27 22:16:29 +00003202InsertValueInst *InsertValueInst::clone_impl() const {
3203 return new InsertValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003204}
3205
Devang Patel11cf3f42009-10-27 22:16:29 +00003206AllocaInst *AllocaInst::clone_impl() const {
3207 return new AllocaInst(getAllocatedType(),
3208 (Value*)getOperand(0),
3209 getAlignment());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003210}
3211
Devang Patel11cf3f42009-10-27 22:16:29 +00003212LoadInst *LoadInst::clone_impl() const {
3213 return new LoadInst(getOperand(0),
3214 Twine(), isVolatile(),
3215 getAlignment());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003216}
3217
Devang Patel11cf3f42009-10-27 22:16:29 +00003218StoreInst *StoreInst::clone_impl() const {
3219 return new StoreInst(getOperand(0), getOperand(1),
3220 isVolatile(), getAlignment());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003221}
3222
Devang Patel11cf3f42009-10-27 22:16:29 +00003223TruncInst *TruncInst::clone_impl() const {
3224 return new TruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003225}
3226
Devang Patel11cf3f42009-10-27 22:16:29 +00003227ZExtInst *ZExtInst::clone_impl() const {
3228 return new ZExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003229}
3230
Devang Patel11cf3f42009-10-27 22:16:29 +00003231SExtInst *SExtInst::clone_impl() const {
3232 return new SExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003233}
3234
Devang Patel11cf3f42009-10-27 22:16:29 +00003235FPTruncInst *FPTruncInst::clone_impl() const {
3236 return new FPTruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003237}
3238
Devang Patel11cf3f42009-10-27 22:16:29 +00003239FPExtInst *FPExtInst::clone_impl() const {
3240 return new FPExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003241}
3242
Devang Patel11cf3f42009-10-27 22:16:29 +00003243UIToFPInst *UIToFPInst::clone_impl() const {
3244 return new UIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003245}
3246
Devang Patel11cf3f42009-10-27 22:16:29 +00003247SIToFPInst *SIToFPInst::clone_impl() const {
3248 return new SIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003249}
3250
Devang Patel11cf3f42009-10-27 22:16:29 +00003251FPToUIInst *FPToUIInst::clone_impl() const {
3252 return new FPToUIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003253}
3254
Devang Patel11cf3f42009-10-27 22:16:29 +00003255FPToSIInst *FPToSIInst::clone_impl() const {
3256 return new FPToSIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003257}
3258
Devang Patel11cf3f42009-10-27 22:16:29 +00003259PtrToIntInst *PtrToIntInst::clone_impl() const {
3260 return new PtrToIntInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003261}
3262
Devang Patel11cf3f42009-10-27 22:16:29 +00003263IntToPtrInst *IntToPtrInst::clone_impl() const {
3264 return new IntToPtrInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003265}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003266
Devang Patel11cf3f42009-10-27 22:16:29 +00003267BitCastInst *BitCastInst::clone_impl() const {
3268 return new BitCastInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003269}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003270
Devang Patel11cf3f42009-10-27 22:16:29 +00003271CallInst *CallInst::clone_impl() const {
3272 return new(getNumOperands()) CallInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003273}
3274
Devang Patel11cf3f42009-10-27 22:16:29 +00003275SelectInst *SelectInst::clone_impl() const {
3276 return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003277}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003278
Devang Patel11cf3f42009-10-27 22:16:29 +00003279VAArgInst *VAArgInst::clone_impl() const {
3280 return new VAArgInst(getOperand(0), getType());
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003281}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003282
Devang Patel11cf3f42009-10-27 22:16:29 +00003283ExtractElementInst *ExtractElementInst::clone_impl() const {
3284 return ExtractElementInst::Create(getOperand(0), getOperand(1));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003285}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003286
Devang Patel11cf3f42009-10-27 22:16:29 +00003287InsertElementInst *InsertElementInst::clone_impl() const {
3288 return InsertElementInst::Create(getOperand(0),
3289 getOperand(1),
3290 getOperand(2));
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003291}
3292
Devang Patel11cf3f42009-10-27 22:16:29 +00003293ShuffleVectorInst *ShuffleVectorInst::clone_impl() const {
3294 return new ShuffleVectorInst(getOperand(0),
3295 getOperand(1),
3296 getOperand(2));
Gabor Greif697e94c2008-05-15 10:04:30 +00003297}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003298
Devang Patel11cf3f42009-10-27 22:16:29 +00003299PHINode *PHINode::clone_impl() const {
3300 return new PHINode(*this);
3301}
3302
3303ReturnInst *ReturnInst::clone_impl() const {
3304 return new(getNumOperands()) ReturnInst(*this);
3305}
3306
3307BranchInst *BranchInst::clone_impl() const {
Jay Foadd81f3c92011-01-07 20:29:02 +00003308 return new(getNumOperands()) BranchInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003309}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003310
Devang Patel11cf3f42009-10-27 22:16:29 +00003311SwitchInst *SwitchInst::clone_impl() const {
3312 return new SwitchInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003313}
3314
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003315IndirectBrInst *IndirectBrInst::clone_impl() const {
3316 return new IndirectBrInst(*this);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003317}
3318
3319
Devang Patel11cf3f42009-10-27 22:16:29 +00003320InvokeInst *InvokeInst::clone_impl() const {
3321 return new(getNumOperands()) InvokeInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003322}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003323
Devang Patel11cf3f42009-10-27 22:16:29 +00003324UnwindInst *UnwindInst::clone_impl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00003325 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00003326 return new UnwindInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003327}
3328
Devang Patel11cf3f42009-10-27 22:16:29 +00003329UnreachableInst *UnreachableInst::clone_impl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00003330 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00003331 return new UnreachableInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003332}