blob: 7226f66e2094052c0e3c90c335d84951cd837e66 [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//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source 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
15#include "llvm/BasicBlock.h"
16#include "llvm/Constants.h"
17#include "llvm/DerivedTypes.h"
18#include "llvm/Function.h"
19#include "llvm/Instructions.h"
Reid Spencerce38beb2007-04-09 18:00:57 +000020#include "llvm/ParameterAttributes.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000021#include "llvm/Support/CallSite.h"
Reid Spencer0286bc12007-02-28 22:00:54 +000022#include "llvm/Support/ConstantRange.h"
Christopher Lamb84485702007-04-22 19:24:39 +000023#include "llvm/Support/MathExtras.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000024using namespace llvm;
25
Chris Lattnerf7b6d312005-05-06 20:26:43 +000026unsigned CallSite::getCallingConv() const {
27 if (CallInst *CI = dyn_cast<CallInst>(I))
28 return CI->getCallingConv();
29 else
30 return cast<InvokeInst>(I)->getCallingConv();
31}
32void CallSite::setCallingConv(unsigned CC) {
33 if (CallInst *CI = dyn_cast<CallInst>(I))
34 CI->setCallingConv(CC);
35 else
36 cast<InvokeInst>(I)->setCallingConv(CC);
37}
Duncan Sandsad0ea2d2007-11-27 13:23:08 +000038const ParamAttrsList* CallSite::getParamAttrs() const {
39 if (CallInst *CI = dyn_cast<CallInst>(I))
40 return CI->getParamAttrs();
41 else
42 return cast<InvokeInst>(I)->getParamAttrs();
43}
44void CallSite::setParamAttrs(const ParamAttrsList *PAL) {
45 if (CallInst *CI = dyn_cast<CallInst>(I))
46 CI->setParamAttrs(PAL);
47 else
48 cast<InvokeInst>(I)->setParamAttrs(PAL);
49}
Chris Lattnerf7b6d312005-05-06 20:26:43 +000050
51
Chris Lattner1c12a882006-06-21 16:53:47 +000052
53
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000054//===----------------------------------------------------------------------===//
Chris Lattnerafdb3de2005-01-29 00:35:16 +000055// TerminatorInst Class
56//===----------------------------------------------------------------------===//
57
Chris Lattner1c12a882006-06-21 16:53:47 +000058// Out of line virtual method, so the vtable, etc has a home.
59TerminatorInst::~TerminatorInst() {
60}
61
62// Out of line virtual method, so the vtable, etc has a home.
63UnaryInstruction::~UnaryInstruction() {
64}
Chris Lattnerafdb3de2005-01-29 00:35:16 +000065
66
67//===----------------------------------------------------------------------===//
68// PHINode Class
69//===----------------------------------------------------------------------===//
70
71PHINode::PHINode(const PHINode &PN)
72 : Instruction(PN.getType(), Instruction::PHI,
73 new Use[PN.getNumOperands()], PN.getNumOperands()),
74 ReservedSpace(PN.getNumOperands()) {
75 Use *OL = OperandList;
76 for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) {
77 OL[i].init(PN.getOperand(i), this);
78 OL[i+1].init(PN.getOperand(i+1), this);
79 }
80}
81
82PHINode::~PHINode() {
83 delete [] OperandList;
84}
85
86// removeIncomingValue - Remove an incoming value. This is useful if a
87// predecessor basic block is deleted.
88Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
89 unsigned NumOps = getNumOperands();
90 Use *OL = OperandList;
91 assert(Idx*2 < NumOps && "BB not in PHI node!");
92 Value *Removed = OL[Idx*2];
93
94 // Move everything after this operand down.
95 //
96 // FIXME: we could just swap with the end of the list, then erase. However,
97 // client might not expect this to happen. The code as it is thrashes the
98 // use/def lists, which is kinda lame.
99 for (unsigned i = (Idx+1)*2; i != NumOps; i += 2) {
100 OL[i-2] = OL[i];
101 OL[i-2+1] = OL[i+1];
102 }
103
104 // Nuke the last value.
105 OL[NumOps-2].set(0);
106 OL[NumOps-2+1].set(0);
107 NumOperands = NumOps-2;
108
109 // If the PHI node is dead, because it has zero entries, nuke it now.
110 if (NumOps == 2 && DeletePHIIfEmpty) {
111 // If anyone is using this PHI, make them use a dummy value instead...
112 replaceAllUsesWith(UndefValue::get(getType()));
113 eraseFromParent();
114 }
115 return Removed;
116}
117
118/// resizeOperands - resize operands - This adjusts the length of the operands
119/// list according to the following behavior:
120/// 1. If NumOps == 0, grow the operand list in response to a push_back style
121/// of operation. This grows the number of ops by 1.5 times.
122/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
123/// 3. If NumOps == NumOperands, trim the reserved space.
124///
125void PHINode::resizeOperands(unsigned NumOps) {
126 if (NumOps == 0) {
127 NumOps = (getNumOperands())*3/2;
128 if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common.
129 } else if (NumOps*2 > NumOperands) {
130 // No resize needed.
131 if (ReservedSpace >= NumOps) return;
132 } else if (NumOps == NumOperands) {
133 if (ReservedSpace == NumOps) return;
134 } else {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000135 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000136 }
137
138 ReservedSpace = NumOps;
139 Use *NewOps = new Use[NumOps];
140 Use *OldOps = OperandList;
141 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
142 NewOps[i].init(OldOps[i], this);
143 OldOps[i].set(0);
144 }
145 delete [] OldOps;
146 OperandList = NewOps;
147}
148
Nate Begemanb3923212005-08-04 23:24:19 +0000149/// hasConstantValue - If the specified PHI node always merges together the same
150/// value, return the value, otherwise return null.
151///
Chris Lattner1d8b2482005-08-05 00:49:06 +0000152Value *PHINode::hasConstantValue(bool AllowNonDominatingInstruction) const {
Nate Begemanb3923212005-08-04 23:24:19 +0000153 // If the PHI node only has one incoming value, eliminate the PHI node...
154 if (getNumIncomingValues() == 1)
Chris Lattner6e709c12005-08-05 15:37:31 +0000155 if (getIncomingValue(0) != this) // not X = phi X
156 return getIncomingValue(0);
157 else
158 return UndefValue::get(getType()); // Self cycle is dead.
159
Nate Begemanb3923212005-08-04 23:24:19 +0000160 // Otherwise if all of the incoming values are the same for the PHI, replace
161 // the PHI node with the incoming value.
162 //
163 Value *InVal = 0;
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000164 bool HasUndefInput = false;
Nate Begemanb3923212005-08-04 23:24:19 +0000165 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i)
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000166 if (isa<UndefValue>(getIncomingValue(i)))
167 HasUndefInput = true;
168 else if (getIncomingValue(i) != this) // Not the PHI node itself...
Nate Begemanb3923212005-08-04 23:24:19 +0000169 if (InVal && getIncomingValue(i) != InVal)
170 return 0; // Not the same, bail out.
171 else
172 InVal = getIncomingValue(i);
173
174 // The only case that could cause InVal to be null is if we have a PHI node
175 // that only has entries for itself. In this case, there is no entry into the
176 // loop, so kill the PHI.
177 //
178 if (InVal == 0) InVal = UndefValue::get(getType());
179
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000180 // If we have a PHI node like phi(X, undef, X), where X is defined by some
181 // instruction, we cannot always return X as the result of the PHI node. Only
182 // do this if X is not an instruction (thus it must dominate the PHI block),
183 // or if the client is prepared to deal with this possibility.
184 if (HasUndefInput && !AllowNonDominatingInstruction)
185 if (Instruction *IV = dyn_cast<Instruction>(InVal))
186 // If it's in the entry block, it dominates everything.
Dan Gohmandcb291f2007-03-22 16:38:57 +0000187 if (IV->getParent() != &IV->getParent()->getParent()->getEntryBlock() ||
Chris Lattner37774af2005-08-05 01:03:27 +0000188 isa<InvokeInst>(IV))
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000189 return 0; // Cannot guarantee that InVal dominates this PHINode.
190
Nate Begemanb3923212005-08-04 23:24:19 +0000191 // All of the incoming values are the same, return the value now.
192 return InVal;
193}
194
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000195
196//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000197// CallInst Implementation
198//===----------------------------------------------------------------------===//
199
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000200CallInst::~CallInst() {
201 delete [] OperandList;
Reid Spencerc6a83842007-04-22 17:28:03 +0000202 if (ParamAttrs)
203 ParamAttrs->dropRef();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000204}
205
Chris Lattner054ba2c2007-02-13 00:58:44 +0000206void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) {
Reid Spencer019c8862007-04-09 15:01:12 +0000207 ParamAttrs = 0;
Chris Lattner054ba2c2007-02-13 00:58:44 +0000208 NumOperands = NumParams+1;
209 Use *OL = OperandList = new Use[NumParams+1];
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000210 OL[0].init(Func, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000211
Misha Brukmanb1c93172005-04-21 23:48:37 +0000212 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000213 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000214 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000215
Chris Lattner054ba2c2007-02-13 00:58:44 +0000216 assert((NumParams == FTy->getNumParams() ||
217 (FTy->isVarArg() && NumParams > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000218 "Calling a function with bad signature!");
Chris Lattner054ba2c2007-02-13 00:58:44 +0000219 for (unsigned i = 0; i != NumParams; ++i) {
Chris Lattner667a0562006-05-03 00:48:22 +0000220 assert((i >= FTy->getNumParams() ||
221 FTy->getParamType(i) == Params[i]->getType()) &&
222 "Calling a function with a bad signature!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000223 OL[i+1].init(Params[i], this);
Chris Lattner667a0562006-05-03 00:48:22 +0000224 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000225}
226
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000227void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
Reid Spencer019c8862007-04-09 15:01:12 +0000228 ParamAttrs = 0;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000229 NumOperands = 3;
230 Use *OL = OperandList = new Use[3];
231 OL[0].init(Func, this);
232 OL[1].init(Actual1, this);
233 OL[2].init(Actual2, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000234
235 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000236 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000237 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000238
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000239 assert((FTy->getNumParams() == 2 ||
Chris Lattner667a0562006-05-03 00:48:22 +0000240 (FTy->isVarArg() && FTy->getNumParams() < 2)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000241 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000242 assert((0 >= FTy->getNumParams() ||
243 FTy->getParamType(0) == Actual1->getType()) &&
244 "Calling a function with a bad signature!");
245 assert((1 >= FTy->getNumParams() ||
246 FTy->getParamType(1) == Actual2->getType()) &&
247 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000248}
249
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000250void CallInst::init(Value *Func, Value *Actual) {
Reid Spencer019c8862007-04-09 15:01:12 +0000251 ParamAttrs = 0;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000252 NumOperands = 2;
253 Use *OL = OperandList = new Use[2];
254 OL[0].init(Func, this);
255 OL[1].init(Actual, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000256
257 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000258 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000259 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000260
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000261 assert((FTy->getNumParams() == 1 ||
262 (FTy->isVarArg() && FTy->getNumParams() == 0)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000263 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000264 assert((0 == FTy->getNumParams() ||
265 FTy->getParamType(0) == Actual->getType()) &&
266 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000267}
268
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000269void CallInst::init(Value *Func) {
Reid Spencer019c8862007-04-09 15:01:12 +0000270 ParamAttrs = 0;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000271 NumOperands = 1;
272 Use *OL = OperandList = new Use[1];
273 OL[0].init(Func, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000274
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000275 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000276 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000277 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000278
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000279 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000280}
281
David Greene17a5dfe2007-08-01 03:43:44 +0000282#if 0
283// Leave for llvm-gcc
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000284CallInst::CallInst(Value *Func, Value* const *Args, unsigned NumArgs,
Misha Brukmanb1c93172005-04-21 23:48:37 +0000285 const std::string &Name, BasicBlock *InsertAtEnd)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000286 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
David Greene17a5dfe2007-08-01 03:43:44 +0000287 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000288 Instruction::Call, 0, 0, InsertAtEnd) {
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000289 init(Func, Args, NumArgs);
Chris Lattner2195fc42007-02-24 00:55:48 +0000290 setName(Name);
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000291}
292CallInst::CallInst(Value *Func, Value* const *Args, unsigned NumArgs,
293 const std::string &Name, Instruction *InsertBefore)
David Greene17a5dfe2007-08-01 03:43:44 +0000294 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
295 ->getElementType())->getReturnType(),
296 Instruction::Call, 0, 0, InsertBefore) {
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000297 init(Func, Args, NumArgs);
Chris Lattner2195fc42007-02-24 00:55:48 +0000298 setName(Name);
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000299}
300
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000301CallInst::CallInst(Value *Func, Value *Actual1, Value *Actual2,
302 const std::string &Name, Instruction *InsertBefore)
303 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
304 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000305 Instruction::Call, 0, 0, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000306 init(Func, Actual1, Actual2);
Chris Lattner2195fc42007-02-24 00:55:48 +0000307 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000308}
309
310CallInst::CallInst(Value *Func, Value *Actual1, Value *Actual2,
311 const std::string &Name, BasicBlock *InsertAtEnd)
312 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
313 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000314 Instruction::Call, 0, 0, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000315 init(Func, Actual1, Actual2);
Chris Lattner2195fc42007-02-24 00:55:48 +0000316 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000317}
David Greene17a5dfe2007-08-01 03:43:44 +0000318#endif
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000319CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +0000320 Instruction *InsertBefore)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000321 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
322 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000323 Instruction::Call, 0, 0, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000324 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000325 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000326}
327
328CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
329 BasicBlock *InsertAtEnd)
330 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
331 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000332 Instruction::Call, 0, 0, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000333 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000334 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000335}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000336CallInst::CallInst(Value *Func, const std::string &Name,
337 Instruction *InsertBefore)
338 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
339 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000340 Instruction::Call, 0, 0, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000341 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000342 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000343}
344
345CallInst::CallInst(Value *Func, const std::string &Name,
346 BasicBlock *InsertAtEnd)
347 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
348 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000349 Instruction::Call, 0, 0, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000350 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000351 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000352}
353
Misha Brukmanb1c93172005-04-21 23:48:37 +0000354CallInst::CallInst(const CallInst &CI)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000355 : Instruction(CI.getType(), Instruction::Call, new Use[CI.getNumOperands()],
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000356 CI.getNumOperands()),
357 ParamAttrs(0) {
358 setParamAttrs(CI.getParamAttrs());
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000359 SubclassData = CI.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000360 Use *OL = OperandList;
361 Use *InOL = CI.OperandList;
362 for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i)
363 OL[i].init(InOL[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000364}
365
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000366void CallInst::setParamAttrs(const ParamAttrsList *newAttrs) {
367 if (ParamAttrs == newAttrs)
368 return;
369
Reid Spencerc6a83842007-04-22 17:28:03 +0000370 if (ParamAttrs)
371 ParamAttrs->dropRef();
372
373 if (newAttrs)
374 newAttrs->addRef();
375
376 ParamAttrs = newAttrs;
377}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000378
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000379bool CallInst::isStructReturn() const {
380 if (ParamAttrs)
381 return ParamAttrs->paramHasAttr(1, ParamAttr::StructRet);
382 return false;
383}
384
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000385//===----------------------------------------------------------------------===//
386// InvokeInst Implementation
387//===----------------------------------------------------------------------===//
388
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000389InvokeInst::~InvokeInst() {
390 delete [] OperandList;
Reid Spencerc6a83842007-04-22 17:28:03 +0000391 if (ParamAttrs)
392 ParamAttrs->dropRef();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000393}
394
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000395void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000396 Value* const *Args, unsigned NumArgs) {
Reid Spencerce38beb2007-04-09 18:00:57 +0000397 ParamAttrs = 0;
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000398 NumOperands = 3+NumArgs;
399 Use *OL = OperandList = new Use[3+NumArgs];
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000400 OL[0].init(Fn, this);
401 OL[1].init(IfNormal, this);
402 OL[2].init(IfException, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000403 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000404 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000405 FTy = FTy; // silence warning.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000406
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000407 assert((NumArgs == FTy->getNumParams()) ||
408 (FTy->isVarArg() && NumArgs > FTy->getNumParams()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000409 "Calling a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000410
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000411 for (unsigned i = 0, e = NumArgs; i != e; i++) {
Chris Lattner667a0562006-05-03 00:48:22 +0000412 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000413 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000414 "Invoking a function with a bad signature!");
415
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000416 OL[i+3].init(Args[i], this);
Chris Lattner667a0562006-05-03 00:48:22 +0000417 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000418}
419
Misha Brukmanb1c93172005-04-21 23:48:37 +0000420InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000421 : TerminatorInst(II.getType(), Instruction::Invoke,
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000422 new Use[II.getNumOperands()], II.getNumOperands()),
423 ParamAttrs(0) {
424 setParamAttrs(II.getParamAttrs());
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000425 SubclassData = II.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000426 Use *OL = OperandList, *InOL = II.OperandList;
427 for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
428 OL[i].init(InOL[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000429}
430
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000431BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
432 return getSuccessor(idx);
433}
434unsigned InvokeInst::getNumSuccessorsV() const {
435 return getNumSuccessors();
436}
437void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
438 return setSuccessor(idx, B);
439}
440
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000441void InvokeInst::setParamAttrs(const ParamAttrsList *newAttrs) {
442 if (ParamAttrs == newAttrs)
443 return;
444
Reid Spencerc6a83842007-04-22 17:28:03 +0000445 if (ParamAttrs)
446 ParamAttrs->dropRef();
447
448 if (newAttrs)
449 newAttrs->addRef();
450
451 ParamAttrs = newAttrs;
452}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000453
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000454bool InvokeInst::isStructReturn() const {
455 if (ParamAttrs)
456 return ParamAttrs->paramHasAttr(1, ParamAttr::StructRet);
457 return false;
458}
459
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000460//===----------------------------------------------------------------------===//
461// ReturnInst Implementation
462//===----------------------------------------------------------------------===//
463
Chris Lattner2195fc42007-02-24 00:55:48 +0000464ReturnInst::ReturnInst(const ReturnInst &RI)
465 : TerminatorInst(Type::VoidTy, Instruction::Ret,
466 &RetVal, RI.getNumOperands()) {
467 if (RI.getNumOperands())
468 RetVal.init(RI.RetVal, this);
469}
470
471ReturnInst::ReturnInst(Value *retVal, Instruction *InsertBefore)
472 : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, 0, InsertBefore) {
473 init(retVal);
474}
475ReturnInst::ReturnInst(Value *retVal, BasicBlock *InsertAtEnd)
476 : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, 0, InsertAtEnd) {
477 init(retVal);
478}
479ReturnInst::ReturnInst(BasicBlock *InsertAtEnd)
480 : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, 0, InsertAtEnd) {
481}
482
483
484
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000485void ReturnInst::init(Value *retVal) {
486 if (retVal && retVal->getType() != Type::VoidTy) {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000487 assert(!isa<BasicBlock>(retVal) &&
Alkis Evlogimenos531e9012004-11-17 21:02:25 +0000488 "Cannot return basic block. Probably using the incorrect ctor");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000489 NumOperands = 1;
490 RetVal.init(retVal, this);
Alkis Evlogimenos531e9012004-11-17 21:02:25 +0000491 }
492}
493
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000494unsigned ReturnInst::getNumSuccessorsV() const {
495 return getNumSuccessors();
496}
497
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000498// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
499// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000500void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000501 assert(0 && "ReturnInst has no successors!");
502}
503
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000504BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
505 assert(0 && "ReturnInst has no successors!");
506 abort();
507 return 0;
508}
509
510
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000511//===----------------------------------------------------------------------===//
512// UnwindInst Implementation
513//===----------------------------------------------------------------------===//
514
Chris Lattner2195fc42007-02-24 00:55:48 +0000515UnwindInst::UnwindInst(Instruction *InsertBefore)
516 : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertBefore) {
517}
518UnwindInst::UnwindInst(BasicBlock *InsertAtEnd)
519 : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertAtEnd) {
520}
521
522
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000523unsigned UnwindInst::getNumSuccessorsV() const {
524 return getNumSuccessors();
525}
526
527void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000528 assert(0 && "UnwindInst has no successors!");
529}
530
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000531BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
532 assert(0 && "UnwindInst has no successors!");
533 abort();
534 return 0;
535}
536
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000537//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000538// UnreachableInst Implementation
539//===----------------------------------------------------------------------===//
540
Chris Lattner2195fc42007-02-24 00:55:48 +0000541UnreachableInst::UnreachableInst(Instruction *InsertBefore)
542 : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertBefore) {
543}
544UnreachableInst::UnreachableInst(BasicBlock *InsertAtEnd)
545 : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertAtEnd) {
546}
547
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000548unsigned UnreachableInst::getNumSuccessorsV() const {
549 return getNumSuccessors();
550}
551
552void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
553 assert(0 && "UnwindInst has no successors!");
554}
555
556BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
557 assert(0 && "UnwindInst has no successors!");
558 abort();
559 return 0;
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000560}
561
562//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000563// BranchInst Implementation
564//===----------------------------------------------------------------------===//
565
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000566void BranchInst::AssertOK() {
567 if (isConditional())
Reid Spencer542964f2007-01-11 18:21:29 +0000568 assert(getCondition()->getType() == Type::Int1Ty &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000569 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000570}
571
Chris Lattner2195fc42007-02-24 00:55:48 +0000572BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
573 : TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 1, InsertBefore) {
574 assert(IfTrue != 0 && "Branch destination may not be null!");
575 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
576}
577BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
578 Instruction *InsertBefore)
579: TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 3, InsertBefore) {
580 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
581 Ops[1].init(reinterpret_cast<Value*>(IfFalse), this);
582 Ops[2].init(Cond, this);
583#ifndef NDEBUG
584 AssertOK();
585#endif
586}
587
588BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
589 : TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 1, InsertAtEnd) {
590 assert(IfTrue != 0 && "Branch destination may not be null!");
591 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
592}
593
594BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
595 BasicBlock *InsertAtEnd)
596 : TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 3, InsertAtEnd) {
597 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
598 Ops[1].init(reinterpret_cast<Value*>(IfFalse), this);
599 Ops[2].init(Cond, this);
600#ifndef NDEBUG
601 AssertOK();
602#endif
603}
604
605
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000606BranchInst::BranchInst(const BranchInst &BI) :
Chris Lattner2195fc42007-02-24 00:55:48 +0000607 TerminatorInst(Type::VoidTy, Instruction::Br, Ops, BI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000608 OperandList[0].init(BI.getOperand(0), this);
609 if (BI.getNumOperands() != 1) {
610 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
611 OperandList[1].init(BI.getOperand(1), this);
612 OperandList[2].init(BI.getOperand(2), this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000613 }
614}
615
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000616BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
617 return getSuccessor(idx);
618}
619unsigned BranchInst::getNumSuccessorsV() const {
620 return getNumSuccessors();
621}
622void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
623 setSuccessor(idx, B);
624}
625
626
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000627//===----------------------------------------------------------------------===//
628// AllocationInst Implementation
629//===----------------------------------------------------------------------===//
630
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000631static Value *getAISize(Value *Amt) {
632 if (!Amt)
Reid Spencer8d9336d2006-12-31 05:26:44 +0000633 Amt = ConstantInt::get(Type::Int32Ty, 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000634 else {
635 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +0000636 "Passed basic block into allocation size parameter! Use other ctor");
Reid Spencer8d9336d2006-12-31 05:26:44 +0000637 assert(Amt->getType() == Type::Int32Ty &&
Reid Spencer7e16e232007-01-26 06:30:34 +0000638 "Malloc/Allocation array size is not a 32-bit integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000639 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000640 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000641}
642
Misha Brukmanb1c93172005-04-21 23:48:37 +0000643AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000644 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000645 Instruction *InsertBefore)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000646 : UnaryInstruction(PointerType::get(Ty), iTy, getAISize(ArraySize),
Chris Lattner2195fc42007-02-24 00:55:48 +0000647 InsertBefore), Alignment(Align) {
Chris Lattner79b8c792005-11-05 21:57:54 +0000648 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000649 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000650 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000651}
652
Misha Brukmanb1c93172005-04-21 23:48:37 +0000653AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000654 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000655 BasicBlock *InsertAtEnd)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000656 : UnaryInstruction(PointerType::get(Ty), iTy, getAISize(ArraySize),
Chris Lattner2195fc42007-02-24 00:55:48 +0000657 InsertAtEnd), Alignment(Align) {
Chris Lattner79b8c792005-11-05 21:57:54 +0000658 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000659 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000660 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000661}
662
Chris Lattner1c12a882006-06-21 16:53:47 +0000663// Out of line virtual method, so the vtable, etc has a home.
664AllocationInst::~AllocationInst() {
665}
666
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000667bool AllocationInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +0000668 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
669 return CI->getZExtValue() != 1;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000670 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000671}
672
673const Type *AllocationInst::getAllocatedType() const {
674 return getType()->getElementType();
675}
676
677AllocaInst::AllocaInst(const AllocaInst &AI)
678 : AllocationInst(AI.getType()->getElementType(), (Value*)AI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000679 Instruction::Alloca, AI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000680}
681
682MallocInst::MallocInst(const MallocInst &MI)
683 : AllocationInst(MI.getType()->getElementType(), (Value*)MI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000684 Instruction::Malloc, MI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000685}
686
687//===----------------------------------------------------------------------===//
688// FreeInst Implementation
689//===----------------------------------------------------------------------===//
690
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000691void FreeInst::AssertOK() {
692 assert(isa<PointerType>(getOperand(0)->getType()) &&
693 "Can not free something of nonpointer type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000694}
695
696FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +0000697 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000698 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000699}
700
701FreeInst::FreeInst(Value *Ptr, BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +0000702 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000703 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000704}
705
706
707//===----------------------------------------------------------------------===//
708// LoadInst Implementation
709//===----------------------------------------------------------------------===//
710
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000711void LoadInst::AssertOK() {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000712 assert(isa<PointerType>(getOperand(0)->getType()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000713 "Ptr must have pointer type.");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000714}
715
716LoadInst::LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000717 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000718 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000719 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000720 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000721 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000722 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000723}
724
725LoadInst::LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000726 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000727 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000728 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000729 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000730 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000731 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000732}
733
734LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
735 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000736 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000737 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000738 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000739 setAlignment(0);
740 AssertOK();
741 setName(Name);
742}
743
744LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
745 unsigned Align, Instruction *InsertBef)
746 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
747 Load, Ptr, InsertBef) {
748 setVolatile(isVolatile);
749 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000750 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000751 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000752}
753
Dan Gohman68659282007-07-18 20:51:11 +0000754LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
755 unsigned Align, BasicBlock *InsertAE)
756 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
757 Load, Ptr, InsertAE) {
758 setVolatile(isVolatile);
759 setAlignment(Align);
760 AssertOK();
761 setName(Name);
762}
763
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000764LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
765 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000766 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000767 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000768 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000769 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000770 AssertOK();
771 setName(Name);
772}
773
774
775
776LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
Chris Lattner2195fc42007-02-24 00:55:48 +0000777 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
778 Load, Ptr, InsertBef) {
Chris Lattner0f048162007-02-13 07:54:42 +0000779 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000780 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000781 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000782 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000783}
784
785LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000786 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
787 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000788 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000789 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000790 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000791 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000792}
793
794LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
795 Instruction *InsertBef)
796: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000797 Load, Ptr, InsertBef) {
Chris Lattner0f048162007-02-13 07:54:42 +0000798 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000799 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000800 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000801 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000802}
803
804LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
805 BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000806 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
807 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000808 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000809 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000810 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000811 if (Name && Name[0]) setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000812}
813
Christopher Lamb84485702007-04-22 19:24:39 +0000814void LoadInst::setAlignment(unsigned Align) {
815 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
816 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
817}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000818
819//===----------------------------------------------------------------------===//
820// StoreInst Implementation
821//===----------------------------------------------------------------------===//
822
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000823void StoreInst::AssertOK() {
824 assert(isa<PointerType>(getOperand(1)->getType()) &&
825 "Ptr must have pointer type!");
826 assert(getOperand(0)->getType() ==
827 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +0000828 && "Ptr must be a pointer to Val type!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000829}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000830
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000831
832StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +0000833 : Instruction(Type::VoidTy, Store, Ops, 2, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000834 Ops[0].init(val, this);
835 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000836 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000837 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000838 AssertOK();
839}
840
841StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +0000842 : Instruction(Type::VoidTy, Store, Ops, 2, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000843 Ops[0].init(val, this);
844 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000845 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000846 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000847 AssertOK();
848}
849
Misha Brukmanb1c93172005-04-21 23:48:37 +0000850StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000851 Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +0000852 : Instruction(Type::VoidTy, Store, Ops, 2, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000853 Ops[0].init(val, this);
854 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000855 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000856 setAlignment(0);
857 AssertOK();
858}
859
860StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
861 unsigned Align, Instruction *InsertBefore)
862 : Instruction(Type::VoidTy, Store, Ops, 2, InsertBefore) {
863 Ops[0].init(val, this);
864 Ops[1].init(addr, this);
865 setVolatile(isVolatile);
866 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000867 AssertOK();
868}
869
Misha Brukmanb1c93172005-04-21 23:48:37 +0000870StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +0000871 unsigned Align, BasicBlock *InsertAtEnd)
872 : Instruction(Type::VoidTy, Store, Ops, 2, InsertAtEnd) {
873 Ops[0].init(val, this);
874 Ops[1].init(addr, this);
875 setVolatile(isVolatile);
876 setAlignment(Align);
877 AssertOK();
878}
879
880StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000881 BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +0000882 : Instruction(Type::VoidTy, Store, Ops, 2, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000883 Ops[0].init(val, this);
884 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000885 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000886 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000887 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000888}
889
Christopher Lamb84485702007-04-22 19:24:39 +0000890void StoreInst::setAlignment(unsigned Align) {
891 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
892 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
893}
894
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000895//===----------------------------------------------------------------------===//
896// GetElementPtrInst Implementation
897//===----------------------------------------------------------------------===//
898
Chris Lattner79807c3d2007-01-31 19:47:18 +0000899void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx) {
900 NumOperands = 1+NumIdx;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000901 Use *OL = OperandList = new Use[NumOperands];
902 OL[0].init(Ptr, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000903
Chris Lattner79807c3d2007-01-31 19:47:18 +0000904 for (unsigned i = 0; i != NumIdx; ++i)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000905 OL[i+1].init(Idx[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000906}
907
Chris Lattner82981202005-05-03 05:43:30 +0000908void GetElementPtrInst::init(Value *Ptr, Value *Idx) {
909 NumOperands = 2;
910 Use *OL = OperandList = new Use[2];
911 OL[0].init(Ptr, this);
912 OL[1].init(Idx, this);
913}
914
Chris Lattner82981202005-05-03 05:43:30 +0000915GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
916 const std::string &Name, Instruction *InBe)
Chris Lattner2195fc42007-02-24 00:55:48 +0000917 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx))),
918 GetElementPtr, 0, 0, InBe) {
Chris Lattner82981202005-05-03 05:43:30 +0000919 init(Ptr, Idx);
Chris Lattner2195fc42007-02-24 00:55:48 +0000920 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +0000921}
922
923GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
924 const std::string &Name, BasicBlock *IAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000925 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx))),
926 GetElementPtr, 0, 0, IAE) {
Chris Lattner82981202005-05-03 05:43:30 +0000927 init(Ptr, Idx);
Chris Lattner2195fc42007-02-24 00:55:48 +0000928 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +0000929}
930
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000931GetElementPtrInst::~GetElementPtrInst() {
932 delete[] OperandList;
933}
934
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000935// getIndexedType - Returns the type of the element that would be loaded with
936// a load instruction with the specified parameters.
937//
Misha Brukmanb1c93172005-04-21 23:48:37 +0000938// A null type is returned if the indices are invalid for the specified
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000939// pointer type.
940//
Misha Brukmanb1c93172005-04-21 23:48:37 +0000941const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
Chris Lattner302116a2007-01-31 04:40:28 +0000942 Value* const *Idxs,
943 unsigned NumIdx,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000944 bool AllowCompositeLeaf) {
945 if (!isa<PointerType>(Ptr)) return 0; // Type isn't a pointer type!
946
947 // Handle the special case of the empty set index set...
Chris Lattner302116a2007-01-31 04:40:28 +0000948 if (NumIdx == 0)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000949 if (AllowCompositeLeaf ||
950 cast<PointerType>(Ptr)->getElementType()->isFirstClassType())
951 return cast<PointerType>(Ptr)->getElementType();
952 else
953 return 0;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000954
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000955 unsigned CurIdx = 0;
956 while (const CompositeType *CT = dyn_cast<CompositeType>(Ptr)) {
Chris Lattner302116a2007-01-31 04:40:28 +0000957 if (NumIdx == CurIdx) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000958 if (AllowCompositeLeaf || CT->isFirstClassType()) return Ptr;
959 return 0; // Can't load a whole structure or array!?!?
960 }
961
Chris Lattner302116a2007-01-31 04:40:28 +0000962 Value *Index = Idxs[CurIdx++];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000963 if (isa<PointerType>(CT) && CurIdx != 1)
964 return 0; // Can only index into pointer types at the first index!
965 if (!CT->indexValid(Index)) return 0;
966 Ptr = CT->getTypeAtIndex(Index);
967
968 // If the new type forwards to another type, then it is in the middle
969 // of being refined to another type (and hence, may have dropped all
970 // references to what it was using before). So, use the new forwarded
971 // type.
972 if (const Type * Ty = Ptr->getForwardedType()) {
973 Ptr = Ty;
974 }
975 }
Chris Lattner302116a2007-01-31 04:40:28 +0000976 return CurIdx == NumIdx ? Ptr : 0;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000977}
978
Chris Lattner82981202005-05-03 05:43:30 +0000979const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
980 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
981 if (!PTy) return 0; // Type isn't a pointer type!
982
983 // Check the pointer index.
984 if (!PTy->indexValid(Idx)) return 0;
985
Chris Lattnerc2233332005-05-03 16:44:45 +0000986 return PTy->getElementType();
Chris Lattner82981202005-05-03 05:43:30 +0000987}
988
Chris Lattner45f15572007-04-14 00:12:57 +0000989
990/// hasAllZeroIndices - Return true if all of the indices of this GEP are
991/// zeros. If so, the result pointer and the first operand have the same
992/// value, just potentially different types.
993bool GetElementPtrInst::hasAllZeroIndices() const {
994 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
995 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
996 if (!CI->isZero()) return false;
997 } else {
998 return false;
999 }
1000 }
1001 return true;
1002}
1003
Chris Lattner27058292007-04-27 20:35:56 +00001004/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1005/// constant integers. If so, the result pointer and the first operand have
1006/// a constant offset between them.
1007bool GetElementPtrInst::hasAllConstantIndices() const {
1008 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1009 if (!isa<ConstantInt>(getOperand(i)))
1010 return false;
1011 }
1012 return true;
1013}
1014
Chris Lattner45f15572007-04-14 00:12:57 +00001015
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001016//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001017// ExtractElementInst Implementation
1018//===----------------------------------------------------------------------===//
1019
1020ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001021 const std::string &Name,
1022 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001023 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001024 ExtractElement, Ops, 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001025 assert(isValidOperands(Val, Index) &&
1026 "Invalid extractelement instruction operands!");
Robert Bocchino23004482006-01-10 19:05:34 +00001027 Ops[0].init(Val, this);
1028 Ops[1].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001029 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001030}
1031
Chris Lattner65511ff2006-10-05 06:24:58 +00001032ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
1033 const std::string &Name,
1034 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001035 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001036 ExtractElement, Ops, 2, InsertBef) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001037 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001038 assert(isValidOperands(Val, Index) &&
1039 "Invalid extractelement instruction operands!");
1040 Ops[0].init(Val, this);
1041 Ops[1].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001042 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001043}
1044
1045
Robert Bocchino23004482006-01-10 19:05:34 +00001046ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001047 const std::string &Name,
1048 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001049 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001050 ExtractElement, Ops, 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001051 assert(isValidOperands(Val, Index) &&
1052 "Invalid extractelement instruction operands!");
1053
Robert Bocchino23004482006-01-10 19:05:34 +00001054 Ops[0].init(Val, this);
1055 Ops[1].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001056 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001057}
1058
Chris Lattner65511ff2006-10-05 06:24:58 +00001059ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
1060 const std::string &Name,
1061 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001062 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001063 ExtractElement, Ops, 2, InsertAE) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001064 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001065 assert(isValidOperands(Val, Index) &&
1066 "Invalid extractelement instruction operands!");
1067
1068 Ops[0].init(Val, this);
1069 Ops[1].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001070 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001071}
1072
1073
Chris Lattner54865b32006-04-08 04:05:48 +00001074bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001075 if (!isa<VectorType>(Val->getType()) || Index->getType() != Type::Int32Ty)
Chris Lattner54865b32006-04-08 04:05:48 +00001076 return false;
1077 return true;
1078}
1079
1080
Robert Bocchino23004482006-01-10 19:05:34 +00001081//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001082// InsertElementInst Implementation
1083//===----------------------------------------------------------------------===//
1084
Chris Lattner0875d942006-04-14 22:20:32 +00001085InsertElementInst::InsertElementInst(const InsertElementInst &IE)
1086 : Instruction(IE.getType(), InsertElement, Ops, 3) {
1087 Ops[0].init(IE.Ops[0], this);
1088 Ops[1].init(IE.Ops[1], this);
1089 Ops[2].init(IE.Ops[2], this);
1090}
Chris Lattner54865b32006-04-08 04:05:48 +00001091InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001092 const std::string &Name,
1093 Instruction *InsertBef)
Chris Lattner2195fc42007-02-24 00:55:48 +00001094 : Instruction(Vec->getType(), InsertElement, Ops, 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001095 assert(isValidOperands(Vec, Elt, Index) &&
1096 "Invalid insertelement instruction operands!");
1097 Ops[0].init(Vec, this);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001098 Ops[1].init(Elt, this);
1099 Ops[2].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001100 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001101}
1102
Chris Lattner65511ff2006-10-05 06:24:58 +00001103InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
1104 const std::string &Name,
1105 Instruction *InsertBef)
Chris Lattner2195fc42007-02-24 00:55:48 +00001106 : Instruction(Vec->getType(), InsertElement, Ops, 3, InsertBef) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001107 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001108 assert(isValidOperands(Vec, Elt, Index) &&
1109 "Invalid insertelement instruction operands!");
1110 Ops[0].init(Vec, this);
1111 Ops[1].init(Elt, this);
1112 Ops[2].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001113 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001114}
1115
1116
Chris Lattner54865b32006-04-08 04:05:48 +00001117InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001118 const std::string &Name,
1119 BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +00001120 : Instruction(Vec->getType(), InsertElement, Ops, 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001121 assert(isValidOperands(Vec, Elt, Index) &&
1122 "Invalid insertelement instruction operands!");
1123
1124 Ops[0].init(Vec, this);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001125 Ops[1].init(Elt, this);
1126 Ops[2].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001127 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001128}
1129
Chris Lattner65511ff2006-10-05 06:24:58 +00001130InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
1131 const std::string &Name,
1132 BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +00001133: Instruction(Vec->getType(), InsertElement, Ops, 3, InsertAE) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001134 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001135 assert(isValidOperands(Vec, Elt, Index) &&
1136 "Invalid insertelement instruction operands!");
1137
1138 Ops[0].init(Vec, this);
1139 Ops[1].init(Elt, this);
1140 Ops[2].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001141 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001142}
1143
Chris Lattner54865b32006-04-08 04:05:48 +00001144bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1145 const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001146 if (!isa<VectorType>(Vec->getType()))
Reid Spencer09575ba2007-02-15 03:39:18 +00001147 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001148
Reid Spencerd84d35b2007-02-15 02:26:10 +00001149 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001150 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001151
Reid Spencer8d9336d2006-12-31 05:26:44 +00001152 if (Index->getType() != Type::Int32Ty)
Chris Lattner54865b32006-04-08 04:05:48 +00001153 return false; // Third operand of insertelement must be uint.
1154 return true;
1155}
1156
1157
Robert Bocchinoca27f032006-01-17 20:07:22 +00001158//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001159// ShuffleVectorInst Implementation
1160//===----------------------------------------------------------------------===//
1161
Chris Lattner0875d942006-04-14 22:20:32 +00001162ShuffleVectorInst::ShuffleVectorInst(const ShuffleVectorInst &SV)
1163 : Instruction(SV.getType(), ShuffleVector, Ops, 3) {
1164 Ops[0].init(SV.Ops[0], this);
1165 Ops[1].init(SV.Ops[1], this);
1166 Ops[2].init(SV.Ops[2], this);
1167}
1168
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001169ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1170 const std::string &Name,
1171 Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +00001172 : Instruction(V1->getType(), ShuffleVector, Ops, 3, InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001173 assert(isValidOperands(V1, V2, Mask) &&
1174 "Invalid shuffle vector instruction operands!");
1175 Ops[0].init(V1, this);
1176 Ops[1].init(V2, this);
1177 Ops[2].init(Mask, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001178 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001179}
1180
1181ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1182 const std::string &Name,
1183 BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +00001184 : Instruction(V1->getType(), ShuffleVector, Ops, 3, InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001185 assert(isValidOperands(V1, V2, Mask) &&
1186 "Invalid shuffle vector instruction operands!");
1187
1188 Ops[0].init(V1, this);
1189 Ops[1].init(V2, this);
1190 Ops[2].init(Mask, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001191 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001192}
1193
1194bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
1195 const Value *Mask) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001196 if (!isa<VectorType>(V1->getType())) return false;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001197 if (V1->getType() != V2->getType()) return false;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001198 if (!isa<VectorType>(Mask->getType()) ||
1199 cast<VectorType>(Mask->getType())->getElementType() != Type::Int32Ty ||
1200 cast<VectorType>(Mask->getType())->getNumElements() !=
1201 cast<VectorType>(V1->getType())->getNumElements())
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001202 return false;
1203 return true;
1204}
1205
1206
1207//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001208// BinaryOperator Class
1209//===----------------------------------------------------------------------===//
1210
Chris Lattner2195fc42007-02-24 00:55:48 +00001211BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1212 const Type *Ty, const std::string &Name,
1213 Instruction *InsertBefore)
1214 : Instruction(Ty, iType, Ops, 2, InsertBefore) {
1215 Ops[0].init(S1, this);
1216 Ops[1].init(S2, this);
1217 init(iType);
1218 setName(Name);
1219}
1220
1221BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1222 const Type *Ty, const std::string &Name,
1223 BasicBlock *InsertAtEnd)
1224 : Instruction(Ty, iType, Ops, 2, InsertAtEnd) {
1225 Ops[0].init(S1, this);
1226 Ops[1].init(S2, this);
1227 init(iType);
1228 setName(Name);
1229}
1230
1231
1232void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001233 Value *LHS = getOperand(0), *RHS = getOperand(1);
Chris Lattnerf14c76c2007-02-01 04:59:37 +00001234 LHS = LHS; RHS = RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001235 assert(LHS->getType() == RHS->getType() &&
1236 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001237#ifndef NDEBUG
1238 switch (iType) {
1239 case Add: case Sub:
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001240 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001241 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001242 "Arithmetic operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001243 assert((getType()->isInteger() || getType()->isFloatingPoint() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001244 isa<VectorType>(getType())) &&
Brian Gaeke02209042004-08-20 06:00:58 +00001245 "Tried to create an arithmetic operation on a non-arithmetic type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001246 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001247 case UDiv:
1248 case SDiv:
1249 assert(getType() == LHS->getType() &&
1250 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001251 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1252 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001253 "Incorrect operand type (not integer) for S/UDIV");
1254 break;
1255 case FDiv:
1256 assert(getType() == LHS->getType() &&
1257 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001258 assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) &&
1259 cast<VectorType>(getType())->getElementType()->isFloatingPoint()))
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001260 && "Incorrect operand type (not floating point) for FDIV");
1261 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001262 case URem:
1263 case SRem:
1264 assert(getType() == LHS->getType() &&
1265 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001266 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1267 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001268 "Incorrect operand type (not integer) for S/UREM");
1269 break;
1270 case FRem:
1271 assert(getType() == LHS->getType() &&
1272 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001273 assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) &&
1274 cast<VectorType>(getType())->getElementType()->isFloatingPoint()))
Reid Spencer7eb55b32006-11-02 01:53:59 +00001275 && "Incorrect operand type (not floating point) for FREM");
1276 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001277 case Shl:
1278 case LShr:
1279 case AShr:
1280 assert(getType() == LHS->getType() &&
1281 "Shift operation should return same type as operands!");
1282 assert(getType()->isInteger() &&
1283 "Shift operation requires integer operands");
1284 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001285 case And: case Or:
1286 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001287 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001288 "Logical operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001289 assert((getType()->isInteger() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001290 (isa<VectorType>(getType()) &&
1291 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001292 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001293 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001294 default:
1295 break;
1296 }
1297#endif
1298}
1299
1300BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001301 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001302 Instruction *InsertBefore) {
1303 assert(S1->getType() == S2->getType() &&
1304 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001305 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001306}
1307
1308BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001309 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001310 BasicBlock *InsertAtEnd) {
1311 BinaryOperator *Res = create(Op, S1, S2, Name);
1312 InsertAtEnd->getInstList().push_back(Res);
1313 return Res;
1314}
1315
1316BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
1317 Instruction *InsertBefore) {
Reid Spencer2eadb532007-01-21 00:29:26 +00001318 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1319 return new BinaryOperator(Instruction::Sub,
1320 zero, Op,
1321 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001322}
1323
1324BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
1325 BasicBlock *InsertAtEnd) {
Reid Spencer2eadb532007-01-21 00:29:26 +00001326 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1327 return new BinaryOperator(Instruction::Sub,
1328 zero, Op,
1329 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001330}
1331
1332BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
1333 Instruction *InsertBefore) {
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001334 Constant *C;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001335 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001336 C = ConstantInt::getAllOnesValue(PTy->getElementType());
Reid Spencerd84d35b2007-02-15 02:26:10 +00001337 C = ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), C));
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001338 } else {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001339 C = ConstantInt::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001340 }
1341
1342 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001343 Op->getType(), Name, InsertBefore);
1344}
1345
1346BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
1347 BasicBlock *InsertAtEnd) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001348 Constant *AllOnes;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001349 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001350 // Create a vector of all ones values.
Zhou Sheng75b871f2007-01-11 12:24:14 +00001351 Constant *Elt = ConstantInt::getAllOnesValue(PTy->getElementType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001352 AllOnes =
Reid Spencerd84d35b2007-02-15 02:26:10 +00001353 ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), Elt));
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001354 } else {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001355 AllOnes = ConstantInt::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001356 }
1357
1358 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001359 Op->getType(), Name, InsertAtEnd);
1360}
1361
1362
1363// isConstantAllOnes - Helper function for several functions below
1364static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner1edec382007-06-15 06:04:24 +00001365 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
1366 return CI->isAllOnesValue();
1367 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
1368 return CV->isAllOnesValue();
1369 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001370}
1371
1372bool BinaryOperator::isNeg(const Value *V) {
1373 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1374 if (Bop->getOpcode() == Instruction::Sub)
Reid Spencer2eadb532007-01-21 00:29:26 +00001375 return Bop->getOperand(0) ==
1376 ConstantExpr::getZeroValueForNegationExpr(Bop->getType());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001377 return false;
1378}
1379
1380bool BinaryOperator::isNot(const Value *V) {
1381 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1382 return (Bop->getOpcode() == Instruction::Xor &&
1383 (isConstantAllOnes(Bop->getOperand(1)) ||
1384 isConstantAllOnes(Bop->getOperand(0))));
1385 return false;
1386}
1387
Chris Lattner2c7d1772005-04-24 07:28:37 +00001388Value *BinaryOperator::getNegArgument(Value *BinOp) {
1389 assert(isNeg(BinOp) && "getNegArgument from non-'neg' instruction!");
1390 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001391}
1392
Chris Lattner2c7d1772005-04-24 07:28:37 +00001393const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1394 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001395}
1396
Chris Lattner2c7d1772005-04-24 07:28:37 +00001397Value *BinaryOperator::getNotArgument(Value *BinOp) {
1398 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1399 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1400 Value *Op0 = BO->getOperand(0);
1401 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001402 if (isConstantAllOnes(Op0)) return Op1;
1403
1404 assert(isConstantAllOnes(Op1));
1405 return Op0;
1406}
1407
Chris Lattner2c7d1772005-04-24 07:28:37 +00001408const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1409 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001410}
1411
1412
1413// swapOperands - Exchange the two operands to this instruction. This
1414// instruction is safe to use on any binary instruction and does not
1415// modify the semantics of the instruction. If the instruction is
1416// order dependent (SetLT f.e.) the opcode is changed.
1417//
1418bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001419 if (!isCommutative())
1420 return true; // Can't commute operands
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001421 std::swap(Ops[0], Ops[1]);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001422 return false;
1423}
1424
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001425//===----------------------------------------------------------------------===//
1426// CastInst Class
1427//===----------------------------------------------------------------------===//
1428
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001429// Just determine if this cast only deals with integral->integral conversion.
1430bool CastInst::isIntegerCast() const {
1431 switch (getOpcode()) {
1432 default: return false;
1433 case Instruction::ZExt:
1434 case Instruction::SExt:
1435 case Instruction::Trunc:
1436 return true;
1437 case Instruction::BitCast:
Chris Lattner03c49532007-01-15 02:27:26 +00001438 return getOperand(0)->getType()->isInteger() && getType()->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001439 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001440}
1441
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001442bool CastInst::isLosslessCast() const {
1443 // Only BitCast can be lossless, exit fast if we're not BitCast
1444 if (getOpcode() != Instruction::BitCast)
1445 return false;
1446
1447 // Identity cast is always lossless
1448 const Type* SrcTy = getOperand(0)->getType();
1449 const Type* DstTy = getType();
1450 if (SrcTy == DstTy)
1451 return true;
1452
Reid Spencer8d9336d2006-12-31 05:26:44 +00001453 // Pointer to pointer is always lossless.
1454 if (isa<PointerType>(SrcTy))
1455 return isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001456 return false; // Other types have no identity values
1457}
1458
1459/// This function determines if the CastInst does not require any bits to be
1460/// changed in order to effect the cast. Essentially, it identifies cases where
1461/// no code gen is necessary for the cast, hence the name no-op cast. For
1462/// example, the following are all no-op casts:
1463/// # bitcast uint %X, int
1464/// # bitcast uint* %x, sbyte*
Dan Gohmanfead7972007-05-11 21:43:24 +00001465/// # bitcast vector< 2 x int > %x, vector< 4 x short>
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001466/// # ptrtoint uint* %x, uint ; on 32-bit plaforms only
1467/// @brief Determine if a cast is a no-op.
1468bool CastInst::isNoopCast(const Type *IntPtrTy) const {
1469 switch (getOpcode()) {
1470 default:
1471 assert(!"Invalid CastOp");
1472 case Instruction::Trunc:
1473 case Instruction::ZExt:
1474 case Instruction::SExt:
1475 case Instruction::FPTrunc:
1476 case Instruction::FPExt:
1477 case Instruction::UIToFP:
1478 case Instruction::SIToFP:
1479 case Instruction::FPToUI:
1480 case Instruction::FPToSI:
1481 return false; // These always modify bits
1482 case Instruction::BitCast:
1483 return true; // BitCast never modifies bits.
1484 case Instruction::PtrToInt:
1485 return IntPtrTy->getPrimitiveSizeInBits() ==
1486 getType()->getPrimitiveSizeInBits();
1487 case Instruction::IntToPtr:
1488 return IntPtrTy->getPrimitiveSizeInBits() ==
1489 getOperand(0)->getType()->getPrimitiveSizeInBits();
1490 }
1491}
1492
1493/// This function determines if a pair of casts can be eliminated and what
1494/// opcode should be used in the elimination. This assumes that there are two
1495/// instructions like this:
1496/// * %F = firstOpcode SrcTy %x to MidTy
1497/// * %S = secondOpcode MidTy %F to DstTy
1498/// The function returns a resultOpcode so these two casts can be replaced with:
1499/// * %Replacement = resultOpcode %SrcTy %x to DstTy
1500/// If no such cast is permited, the function returns 0.
1501unsigned CastInst::isEliminableCastPair(
1502 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
1503 const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
1504{
1505 // Define the 144 possibilities for these two cast instructions. The values
1506 // in this matrix determine what to do in a given situation and select the
1507 // case in the switch below. The rows correspond to firstOp, the columns
1508 // correspond to secondOp. In looking at the table below, keep in mind
1509 // the following cast properties:
1510 //
1511 // Size Compare Source Destination
1512 // Operator Src ? Size Type Sign Type Sign
1513 // -------- ------------ ------------------- ---------------------
1514 // TRUNC > Integer Any Integral Any
1515 // ZEXT < Integral Unsigned Integer Any
1516 // SEXT < Integral Signed Integer Any
1517 // FPTOUI n/a FloatPt n/a Integral Unsigned
1518 // FPTOSI n/a FloatPt n/a Integral Signed
1519 // UITOFP n/a Integral Unsigned FloatPt n/a
1520 // SITOFP n/a Integral Signed FloatPt n/a
1521 // FPTRUNC > FloatPt n/a FloatPt n/a
1522 // FPEXT < FloatPt n/a FloatPt n/a
1523 // PTRTOINT n/a Pointer n/a Integral Unsigned
1524 // INTTOPTR n/a Integral Unsigned Pointer n/a
1525 // BITCONVERT = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00001526 //
1527 // NOTE: some transforms are safe, but we consider them to be non-profitable.
1528 // For example, we could merge "fptoui double to uint" + "zext uint to ulong",
1529 // into "fptoui double to ulong", but this loses information about the range
1530 // of the produced value (we no longer know the top-part is all zeros).
1531 // Further this conversion is often much more expensive for typical hardware,
1532 // and causes issues when building libgcc. We disallow fptosi+sext for the
1533 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001534 const unsigned numCastOps =
1535 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
1536 static const uint8_t CastResults[numCastOps][numCastOps] = {
1537 // T F F U S F F P I B -+
1538 // R Z S P P I I T P 2 N T |
1539 // U E E 2 2 2 2 R E I T C +- secondOp
1540 // N X X U S F F N X N 2 V |
1541 // C T T I I P P C T T P T -+
1542 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
1543 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
1544 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00001545 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
1546 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001547 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
1548 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
1549 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
1550 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
1551 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
1552 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
1553 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
1554 };
1555
1556 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
1557 [secondOp-Instruction::CastOpsBegin];
1558 switch (ElimCase) {
1559 case 0:
1560 // categorically disallowed
1561 return 0;
1562 case 1:
1563 // allowed, use first cast's opcode
1564 return firstOp;
1565 case 2:
1566 // allowed, use second cast's opcode
1567 return secondOp;
1568 case 3:
1569 // no-op cast in second op implies firstOp as long as the DestTy
1570 // is integer
Chris Lattner03c49532007-01-15 02:27:26 +00001571 if (DstTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001572 return firstOp;
1573 return 0;
1574 case 4:
1575 // no-op cast in second op implies firstOp as long as the DestTy
1576 // is floating point
1577 if (DstTy->isFloatingPoint())
1578 return firstOp;
1579 return 0;
1580 case 5:
1581 // no-op cast in first op implies secondOp as long as the SrcTy
1582 // is an integer
Chris Lattner03c49532007-01-15 02:27:26 +00001583 if (SrcTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001584 return secondOp;
1585 return 0;
1586 case 6:
1587 // no-op cast in first op implies secondOp as long as the SrcTy
1588 // is a floating point
1589 if (SrcTy->isFloatingPoint())
1590 return secondOp;
1591 return 0;
1592 case 7: {
1593 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
1594 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1595 unsigned MidSize = MidTy->getPrimitiveSizeInBits();
1596 if (MidSize >= PtrSize)
1597 return Instruction::BitCast;
1598 return 0;
1599 }
1600 case 8: {
1601 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
1602 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
1603 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
1604 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1605 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1606 if (SrcSize == DstSize)
1607 return Instruction::BitCast;
1608 else if (SrcSize < DstSize)
1609 return firstOp;
1610 return secondOp;
1611 }
1612 case 9: // zext, sext -> zext, because sext can't sign extend after zext
1613 return Instruction::ZExt;
1614 case 10:
1615 // fpext followed by ftrunc is allowed if the bit size returned to is
1616 // the same as the original, in which case its just a bitcast
1617 if (SrcTy == DstTy)
1618 return Instruction::BitCast;
1619 return 0; // If the types are not the same we can't eliminate it.
1620 case 11:
1621 // bitcast followed by ptrtoint is allowed as long as the bitcast
1622 // is a pointer to pointer cast.
1623 if (isa<PointerType>(SrcTy) && isa<PointerType>(MidTy))
1624 return secondOp;
1625 return 0;
1626 case 12:
1627 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
1628 if (isa<PointerType>(MidTy) && isa<PointerType>(DstTy))
1629 return firstOp;
1630 return 0;
1631 case 13: {
1632 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
1633 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1634 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1635 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1636 if (SrcSize <= PtrSize && SrcSize == DstSize)
1637 return Instruction::BitCast;
1638 return 0;
1639 }
1640 case 99:
1641 // cast combination can't happen (error in input). This is for all cases
1642 // where the MidTy is not the same for the two cast instructions.
1643 assert(!"Invalid Cast Combination");
1644 return 0;
1645 default:
1646 assert(!"Error in CastResults table!!!");
1647 return 0;
1648 }
1649 return 0;
1650}
1651
1652CastInst *CastInst::create(Instruction::CastOps op, Value *S, const Type *Ty,
1653 const std::string &Name, Instruction *InsertBefore) {
1654 // Construct and return the appropriate CastInst subclass
1655 switch (op) {
1656 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
1657 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
1658 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
1659 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
1660 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
1661 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
1662 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
1663 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
1664 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
1665 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
1666 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
1667 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
1668 default:
1669 assert(!"Invalid opcode provided");
1670 }
1671 return 0;
1672}
1673
1674CastInst *CastInst::create(Instruction::CastOps op, Value *S, const Type *Ty,
1675 const std::string &Name, BasicBlock *InsertAtEnd) {
1676 // Construct and return the appropriate CastInst subclass
1677 switch (op) {
1678 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
1679 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
1680 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
1681 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
1682 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
1683 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
1684 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
1685 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
1686 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
1687 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
1688 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
1689 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
1690 default:
1691 assert(!"Invalid opcode provided");
1692 }
1693 return 0;
1694}
1695
Reid Spencer5c140882006-12-04 20:17:56 +00001696CastInst *CastInst::createZExtOrBitCast(Value *S, const Type *Ty,
1697 const std::string &Name,
1698 Instruction *InsertBefore) {
1699 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1700 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1701 return create(Instruction::ZExt, S, Ty, Name, InsertBefore);
1702}
1703
1704CastInst *CastInst::createZExtOrBitCast(Value *S, const Type *Ty,
1705 const std::string &Name,
1706 BasicBlock *InsertAtEnd) {
1707 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1708 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1709 return create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
1710}
1711
1712CastInst *CastInst::createSExtOrBitCast(Value *S, const Type *Ty,
1713 const std::string &Name,
1714 Instruction *InsertBefore) {
1715 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1716 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1717 return create(Instruction::SExt, S, Ty, Name, InsertBefore);
1718}
1719
1720CastInst *CastInst::createSExtOrBitCast(Value *S, const Type *Ty,
1721 const std::string &Name,
1722 BasicBlock *InsertAtEnd) {
1723 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1724 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1725 return create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
1726}
1727
1728CastInst *CastInst::createTruncOrBitCast(Value *S, const Type *Ty,
1729 const std::string &Name,
1730 Instruction *InsertBefore) {
1731 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1732 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1733 return create(Instruction::Trunc, S, Ty, Name, InsertBefore);
1734}
1735
1736CastInst *CastInst::createTruncOrBitCast(Value *S, const Type *Ty,
1737 const std::string &Name,
1738 BasicBlock *InsertAtEnd) {
1739 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1740 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1741 return create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
1742}
1743
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001744CastInst *CastInst::createPointerCast(Value *S, const Type *Ty,
1745 const std::string &Name,
1746 BasicBlock *InsertAtEnd) {
1747 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001748 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001749 "Invalid cast");
1750
Chris Lattner03c49532007-01-15 02:27:26 +00001751 if (Ty->isInteger())
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001752 return create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
1753 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1754}
1755
1756/// @brief Create a BitCast or a PtrToInt cast instruction
1757CastInst *CastInst::createPointerCast(Value *S, const Type *Ty,
1758 const std::string &Name,
1759 Instruction *InsertBefore) {
1760 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001761 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001762 "Invalid cast");
1763
Chris Lattner03c49532007-01-15 02:27:26 +00001764 if (Ty->isInteger())
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001765 return create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
1766 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1767}
1768
Reid Spencer7e933472006-12-12 00:49:44 +00001769CastInst *CastInst::createIntegerCast(Value *C, const Type *Ty,
1770 bool isSigned, const std::string &Name,
1771 Instruction *InsertBefore) {
Chris Lattner03c49532007-01-15 02:27:26 +00001772 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer7e933472006-12-12 00:49:44 +00001773 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1774 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1775 Instruction::CastOps opcode =
1776 (SrcBits == DstBits ? Instruction::BitCast :
1777 (SrcBits > DstBits ? Instruction::Trunc :
1778 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1779 return create(opcode, C, Ty, Name, InsertBefore);
1780}
1781
1782CastInst *CastInst::createIntegerCast(Value *C, const Type *Ty,
1783 bool isSigned, const std::string &Name,
1784 BasicBlock *InsertAtEnd) {
Chris Lattner03c49532007-01-15 02:27:26 +00001785 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer7e933472006-12-12 00:49:44 +00001786 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1787 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1788 Instruction::CastOps opcode =
1789 (SrcBits == DstBits ? Instruction::BitCast :
1790 (SrcBits > DstBits ? Instruction::Trunc :
1791 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1792 return create(opcode, C, Ty, Name, InsertAtEnd);
1793}
1794
1795CastInst *CastInst::createFPCast(Value *C, const Type *Ty,
1796 const std::string &Name,
1797 Instruction *InsertBefore) {
1798 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1799 "Invalid cast");
1800 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1801 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1802 Instruction::CastOps opcode =
1803 (SrcBits == DstBits ? Instruction::BitCast :
1804 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
1805 return create(opcode, C, Ty, Name, InsertBefore);
1806}
1807
1808CastInst *CastInst::createFPCast(Value *C, const Type *Ty,
1809 const std::string &Name,
1810 BasicBlock *InsertAtEnd) {
1811 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1812 "Invalid cast");
1813 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1814 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1815 Instruction::CastOps opcode =
1816 (SrcBits == DstBits ? Instruction::BitCast :
1817 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
1818 return create(opcode, C, Ty, Name, InsertAtEnd);
1819}
1820
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001821// Provide a way to get a "cast" where the cast opcode is inferred from the
1822// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001823// logic in the castIsValid function below. This axiom should hold:
1824// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
1825// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001826// casting opcode for the arguments passed to it.
1827Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00001828CastInst::getCastOpcode(
1829 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001830 // Get the bit sizes, we'll need these
1831 const Type *SrcTy = Src->getType();
Dan Gohmanfead7972007-05-11 21:43:24 +00001832 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
1833 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001834
1835 // Run through the possibilities ...
Chris Lattner03c49532007-01-15 02:27:26 +00001836 if (DestTy->isInteger()) { // Casting to integral
1837 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001838 if (DestBits < SrcBits)
1839 return Trunc; // int -> smaller int
1840 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00001841 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001842 return SExt; // signed -> SEXT
1843 else
1844 return ZExt; // unsigned -> ZEXT
1845 } else {
1846 return BitCast; // Same size, No-op cast
1847 }
1848 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00001849 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001850 return FPToSI; // FP -> sint
1851 else
1852 return FPToUI; // FP -> uint
Reid Spencerd84d35b2007-02-15 02:26:10 +00001853 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001854 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00001855 "Casting vector to integer of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001856 return BitCast; // Same size, no-op cast
1857 } else {
1858 assert(isa<PointerType>(SrcTy) &&
1859 "Casting from a value that is not first-class type");
1860 return PtrToInt; // ptr -> int
1861 }
1862 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
Chris Lattner03c49532007-01-15 02:27:26 +00001863 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00001864 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001865 return SIToFP; // sint -> FP
1866 else
1867 return UIToFP; // uint -> FP
1868 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
1869 if (DestBits < SrcBits) {
1870 return FPTrunc; // FP -> smaller FP
1871 } else if (DestBits > SrcBits) {
1872 return FPExt; // FP -> larger FP
1873 } else {
1874 return BitCast; // same size, no-op cast
1875 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00001876 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001877 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00001878 "Casting vector to floating point of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001879 return BitCast; // same size, no-op cast
1880 } else {
1881 assert(0 && "Casting pointer or non-first class to float");
1882 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00001883 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
1884 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001885 assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00001886 "Casting vector to vector of different widths");
1887 return BitCast; // vector -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001888 } else if (DestPTy->getBitWidth() == SrcBits) {
Dan Gohmanfead7972007-05-11 21:43:24 +00001889 return BitCast; // float/int -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001890 } else {
Dan Gohmanfead7972007-05-11 21:43:24 +00001891 assert(!"Illegal cast to vector (wrong type or size)");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001892 }
1893 } else if (isa<PointerType>(DestTy)) {
1894 if (isa<PointerType>(SrcTy)) {
1895 return BitCast; // ptr -> ptr
Chris Lattner03c49532007-01-15 02:27:26 +00001896 } else if (SrcTy->isInteger()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001897 return IntToPtr; // int -> ptr
1898 } else {
1899 assert(!"Casting pointer to other than pointer or int");
1900 }
1901 } else {
1902 assert(!"Casting to type that is not first-class");
1903 }
1904
1905 // If we fall through to here we probably hit an assertion cast above
1906 // and assertions are not turned on. Anything we return is an error, so
1907 // BitCast is as good a choice as any.
1908 return BitCast;
1909}
1910
1911//===----------------------------------------------------------------------===//
1912// CastInst SubClass Constructors
1913//===----------------------------------------------------------------------===//
1914
1915/// Check that the construction parameters for a CastInst are correct. This
1916/// could be broken out into the separate constructors but it is useful to have
1917/// it in one place and to eliminate the redundant code for getting the sizes
1918/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001919bool
1920CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001921
1922 // Check for type sanity on the arguments
1923 const Type *SrcTy = S->getType();
1924 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType())
1925 return false;
1926
1927 // Get the size of the types in bits, we'll need this later
1928 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
1929 unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
1930
1931 // Switch on the opcode provided
1932 switch (op) {
1933 default: return false; // This is an input error
1934 case Instruction::Trunc:
Chris Lattner03c49532007-01-15 02:27:26 +00001935 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001936 case Instruction::ZExt:
Chris Lattner03c49532007-01-15 02:27:26 +00001937 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001938 case Instruction::SExt:
Chris Lattner03c49532007-01-15 02:27:26 +00001939 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001940 case Instruction::FPTrunc:
1941 return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() &&
1942 SrcBitSize > DstBitSize;
1943 case Instruction::FPExt:
1944 return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() &&
1945 SrcBitSize < DstBitSize;
1946 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001947 case Instruction::SIToFP:
Nate Begemand4d45c22007-11-17 03:58:34 +00001948 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
1949 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
1950 return SVTy->getElementType()->isInteger() &&
1951 DVTy->getElementType()->isFloatingPoint() &&
1952 SVTy->getNumElements() == DVTy->getNumElements();
1953 }
1954 }
Chris Lattner03c49532007-01-15 02:27:26 +00001955 return SrcTy->isInteger() && DstTy->isFloatingPoint();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001956 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001957 case Instruction::FPToSI:
Nate Begemand4d45c22007-11-17 03:58:34 +00001958 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
1959 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
1960 return SVTy->getElementType()->isFloatingPoint() &&
1961 DVTy->getElementType()->isInteger() &&
1962 SVTy->getNumElements() == DVTy->getNumElements();
1963 }
1964 }
Chris Lattner03c49532007-01-15 02:27:26 +00001965 return SrcTy->isFloatingPoint() && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001966 case Instruction::PtrToInt:
Chris Lattner03c49532007-01-15 02:27:26 +00001967 return isa<PointerType>(SrcTy) && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001968 case Instruction::IntToPtr:
Chris Lattner03c49532007-01-15 02:27:26 +00001969 return SrcTy->isInteger() && isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001970 case Instruction::BitCast:
1971 // BitCast implies a no-op cast of type only. No bits change.
1972 // However, you can't cast pointers to anything but pointers.
1973 if (isa<PointerType>(SrcTy) != isa<PointerType>(DstTy))
1974 return false;
1975
1976 // Now we know we're not dealing with a pointer/non-poiner mismatch. In all
1977 // these cases, the cast is okay if the source and destination bit widths
1978 // are identical.
1979 return SrcBitSize == DstBitSize;
1980 }
1981}
1982
1983TruncInst::TruncInst(
1984 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1985) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001986 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001987}
1988
1989TruncInst::TruncInst(
1990 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1991) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001992 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001993}
1994
1995ZExtInst::ZExtInst(
1996 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1997) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001998 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001999}
2000
2001ZExtInst::ZExtInst(
2002 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2003) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002004 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002005}
2006SExtInst::SExtInst(
2007 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2008) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002009 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002010}
2011
Jeff Cohencc08c832006-12-02 02:22:01 +00002012SExtInst::SExtInst(
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002013 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2014) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002015 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002016}
2017
2018FPTruncInst::FPTruncInst(
2019 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2020) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002021 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002022}
2023
2024FPTruncInst::FPTruncInst(
2025 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2026) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002027 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002028}
2029
2030FPExtInst::FPExtInst(
2031 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2032) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002033 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002034}
2035
2036FPExtInst::FPExtInst(
2037 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2038) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002039 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002040}
2041
2042UIToFPInst::UIToFPInst(
2043 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2044) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002045 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002046}
2047
2048UIToFPInst::UIToFPInst(
2049 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2050) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002051 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002052}
2053
2054SIToFPInst::SIToFPInst(
2055 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2056) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002057 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002058}
2059
2060SIToFPInst::SIToFPInst(
2061 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2062) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002063 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002064}
2065
2066FPToUIInst::FPToUIInst(
2067 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2068) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002069 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002070}
2071
2072FPToUIInst::FPToUIInst(
2073 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2074) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002075 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002076}
2077
2078FPToSIInst::FPToSIInst(
2079 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2080) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002081 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002082}
2083
2084FPToSIInst::FPToSIInst(
2085 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2086) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002087 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002088}
2089
2090PtrToIntInst::PtrToIntInst(
2091 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2092) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002093 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002094}
2095
2096PtrToIntInst::PtrToIntInst(
2097 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2098) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002099 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002100}
2101
2102IntToPtrInst::IntToPtrInst(
2103 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2104) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002105 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002106}
2107
2108IntToPtrInst::IntToPtrInst(
2109 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2110) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002111 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002112}
2113
2114BitCastInst::BitCastInst(
2115 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2116) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002117 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002118}
2119
2120BitCastInst::BitCastInst(
2121 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2122) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002123 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002124}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002125
2126//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002127// CmpInst Classes
2128//===----------------------------------------------------------------------===//
2129
2130CmpInst::CmpInst(OtherOps op, unsigned short predicate, Value *LHS, Value *RHS,
2131 const std::string &Name, Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +00002132 : Instruction(Type::Int1Ty, op, Ops, 2, InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002133 Ops[0].init(LHS, this);
2134 Ops[1].init(RHS, this);
2135 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002136 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002137 if (op == Instruction::ICmp) {
2138 assert(predicate >= ICmpInst::FIRST_ICMP_PREDICATE &&
2139 predicate <= ICmpInst::LAST_ICMP_PREDICATE &&
2140 "Invalid ICmp predicate value");
2141 const Type* Op0Ty = getOperand(0)->getType();
2142 const Type* Op1Ty = getOperand(1)->getType();
2143 assert(Op0Ty == Op1Ty &&
2144 "Both operands to ICmp instruction are not of the same type!");
2145 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00002146 assert((Op0Ty->isInteger() || isa<PointerType>(Op0Ty)) &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002147 "Invalid operand types for ICmp instruction");
2148 return;
2149 }
2150 assert(op == Instruction::FCmp && "Invalid CmpInst opcode");
2151 assert(predicate <= FCmpInst::LAST_FCMP_PREDICATE &&
2152 "Invalid FCmp predicate value");
2153 const Type* Op0Ty = getOperand(0)->getType();
2154 const Type* Op1Ty = getOperand(1)->getType();
2155 assert(Op0Ty == Op1Ty &&
2156 "Both operands to FCmp instruction are not of the same type!");
2157 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00002158 assert(Op0Ty->isFloatingPoint() &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002159 "Invalid operand types for FCmp instruction");
2160}
2161
2162CmpInst::CmpInst(OtherOps op, unsigned short predicate, Value *LHS, Value *RHS,
2163 const std::string &Name, BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +00002164 : Instruction(Type::Int1Ty, op, Ops, 2, InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002165 Ops[0].init(LHS, this);
2166 Ops[1].init(RHS, this);
2167 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002168 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002169 if (op == Instruction::ICmp) {
2170 assert(predicate >= ICmpInst::FIRST_ICMP_PREDICATE &&
2171 predicate <= ICmpInst::LAST_ICMP_PREDICATE &&
2172 "Invalid ICmp predicate value");
2173
2174 const Type* Op0Ty = getOperand(0)->getType();
2175 const Type* Op1Ty = getOperand(1)->getType();
2176 assert(Op0Ty == Op1Ty &&
2177 "Both operands to ICmp instruction are not of the same type!");
2178 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00002179 assert(Op0Ty->isInteger() || isa<PointerType>(Op0Ty) &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002180 "Invalid operand types for ICmp instruction");
2181 return;
2182 }
2183 assert(op == Instruction::FCmp && "Invalid CmpInst opcode");
2184 assert(predicate <= FCmpInst::LAST_FCMP_PREDICATE &&
2185 "Invalid FCmp predicate value");
2186 const Type* Op0Ty = getOperand(0)->getType();
2187 const Type* Op1Ty = getOperand(1)->getType();
2188 assert(Op0Ty == Op1Ty &&
2189 "Both operands to FCmp instruction are not of the same type!");
2190 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00002191 assert(Op0Ty->isFloatingPoint() &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002192 "Invalid operand types for FCmp instruction");
2193}
2194
2195CmpInst *
2196CmpInst::create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
2197 const std::string &Name, Instruction *InsertBefore) {
2198 if (Op == Instruction::ICmp) {
2199 return new ICmpInst(ICmpInst::Predicate(predicate), S1, S2, Name,
2200 InsertBefore);
2201 }
2202 return new FCmpInst(FCmpInst::Predicate(predicate), S1, S2, Name,
2203 InsertBefore);
2204}
2205
2206CmpInst *
2207CmpInst::create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
2208 const std::string &Name, BasicBlock *InsertAtEnd) {
2209 if (Op == Instruction::ICmp) {
2210 return new ICmpInst(ICmpInst::Predicate(predicate), S1, S2, Name,
2211 InsertAtEnd);
2212 }
2213 return new FCmpInst(FCmpInst::Predicate(predicate), S1, S2, Name,
2214 InsertAtEnd);
2215}
2216
2217void CmpInst::swapOperands() {
2218 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2219 IC->swapOperands();
2220 else
2221 cast<FCmpInst>(this)->swapOperands();
2222}
2223
2224bool CmpInst::isCommutative() {
2225 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2226 return IC->isCommutative();
2227 return cast<FCmpInst>(this)->isCommutative();
2228}
2229
2230bool CmpInst::isEquality() {
2231 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2232 return IC->isEquality();
2233 return cast<FCmpInst>(this)->isEquality();
2234}
2235
2236
2237ICmpInst::Predicate ICmpInst::getInversePredicate(Predicate pred) {
2238 switch (pred) {
2239 default:
2240 assert(!"Unknown icmp predicate!");
2241 case ICMP_EQ: return ICMP_NE;
2242 case ICMP_NE: return ICMP_EQ;
2243 case ICMP_UGT: return ICMP_ULE;
2244 case ICMP_ULT: return ICMP_UGE;
2245 case ICMP_UGE: return ICMP_ULT;
2246 case ICMP_ULE: return ICMP_UGT;
2247 case ICMP_SGT: return ICMP_SLE;
2248 case ICMP_SLT: return ICMP_SGE;
2249 case ICMP_SGE: return ICMP_SLT;
2250 case ICMP_SLE: return ICMP_SGT;
2251 }
2252}
2253
2254ICmpInst::Predicate ICmpInst::getSwappedPredicate(Predicate pred) {
2255 switch (pred) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002256 default: assert(! "Unknown icmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002257 case ICMP_EQ: case ICMP_NE:
2258 return pred;
2259 case ICMP_SGT: return ICMP_SLT;
2260 case ICMP_SLT: return ICMP_SGT;
2261 case ICMP_SGE: return ICMP_SLE;
2262 case ICMP_SLE: return ICMP_SGE;
2263 case ICMP_UGT: return ICMP_ULT;
2264 case ICMP_ULT: return ICMP_UGT;
2265 case ICMP_UGE: return ICMP_ULE;
2266 case ICMP_ULE: return ICMP_UGE;
2267 }
2268}
2269
Reid Spencer266e42b2006-12-23 06:05:41 +00002270ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2271 switch (pred) {
2272 default: assert(! "Unknown icmp predicate!");
2273 case ICMP_EQ: case ICMP_NE:
2274 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2275 return pred;
2276 case ICMP_UGT: return ICMP_SGT;
2277 case ICMP_ULT: return ICMP_SLT;
2278 case ICMP_UGE: return ICMP_SGE;
2279 case ICMP_ULE: return ICMP_SLE;
2280 }
2281}
2282
2283bool ICmpInst::isSignedPredicate(Predicate pred) {
2284 switch (pred) {
2285 default: assert(! "Unknown icmp predicate!");
2286 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2287 return true;
2288 case ICMP_EQ: case ICMP_NE: case ICMP_UGT: case ICMP_ULT:
2289 case ICMP_UGE: case ICMP_ULE:
2290 return false;
2291 }
2292}
2293
Reid Spencer0286bc12007-02-28 22:00:54 +00002294/// Initialize a set of values that all satisfy the condition with C.
2295///
2296ConstantRange
2297ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2298 APInt Lower(C);
2299 APInt Upper(C);
2300 uint32_t BitWidth = C.getBitWidth();
2301 switch (pred) {
2302 default: assert(0 && "Invalid ICmp opcode to ConstantRange ctor!");
2303 case ICmpInst::ICMP_EQ: Upper++; break;
2304 case ICmpInst::ICMP_NE: Lower++; break;
2305 case ICmpInst::ICMP_ULT: Lower = APInt::getMinValue(BitWidth); break;
2306 case ICmpInst::ICMP_SLT: Lower = APInt::getSignedMinValue(BitWidth); break;
2307 case ICmpInst::ICMP_UGT:
2308 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2309 break;
2310 case ICmpInst::ICMP_SGT:
2311 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2312 break;
2313 case ICmpInst::ICMP_ULE:
2314 Lower = APInt::getMinValue(BitWidth); Upper++;
2315 break;
2316 case ICmpInst::ICMP_SLE:
2317 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
2318 break;
2319 case ICmpInst::ICMP_UGE:
2320 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2321 break;
2322 case ICmpInst::ICMP_SGE:
2323 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2324 break;
2325 }
2326 return ConstantRange(Lower, Upper);
2327}
2328
Reid Spencerd9436b62006-11-20 01:22:35 +00002329FCmpInst::Predicate FCmpInst::getInversePredicate(Predicate pred) {
2330 switch (pred) {
2331 default:
2332 assert(!"Unknown icmp predicate!");
2333 case FCMP_OEQ: return FCMP_UNE;
2334 case FCMP_ONE: return FCMP_UEQ;
2335 case FCMP_OGT: return FCMP_ULE;
2336 case FCMP_OLT: return FCMP_UGE;
2337 case FCMP_OGE: return FCMP_ULT;
2338 case FCMP_OLE: return FCMP_UGT;
2339 case FCMP_UEQ: return FCMP_ONE;
2340 case FCMP_UNE: return FCMP_OEQ;
2341 case FCMP_UGT: return FCMP_OLE;
2342 case FCMP_ULT: return FCMP_OGE;
2343 case FCMP_UGE: return FCMP_OLT;
2344 case FCMP_ULE: return FCMP_OGT;
2345 case FCMP_ORD: return FCMP_UNO;
2346 case FCMP_UNO: return FCMP_ORD;
2347 case FCMP_TRUE: return FCMP_FALSE;
2348 case FCMP_FALSE: return FCMP_TRUE;
2349 }
2350}
2351
2352FCmpInst::Predicate FCmpInst::getSwappedPredicate(Predicate pred) {
2353 switch (pred) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002354 default: assert(!"Unknown fcmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002355 case FCMP_FALSE: case FCMP_TRUE:
2356 case FCMP_OEQ: case FCMP_ONE:
2357 case FCMP_UEQ: case FCMP_UNE:
2358 case FCMP_ORD: case FCMP_UNO:
2359 return pred;
2360 case FCMP_OGT: return FCMP_OLT;
2361 case FCMP_OLT: return FCMP_OGT;
2362 case FCMP_OGE: return FCMP_OLE;
2363 case FCMP_OLE: return FCMP_OGE;
2364 case FCMP_UGT: return FCMP_ULT;
2365 case FCMP_ULT: return FCMP_UGT;
2366 case FCMP_UGE: return FCMP_ULE;
2367 case FCMP_ULE: return FCMP_UGE;
2368 }
2369}
2370
Reid Spencer266e42b2006-12-23 06:05:41 +00002371bool CmpInst::isUnsigned(unsigned short predicate) {
2372 switch (predicate) {
2373 default: return false;
2374 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2375 case ICmpInst::ICMP_UGE: return true;
2376 }
2377}
2378
2379bool CmpInst::isSigned(unsigned short predicate){
2380 switch (predicate) {
2381 default: return false;
2382 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2383 case ICmpInst::ICMP_SGE: return true;
2384 }
2385}
2386
2387bool CmpInst::isOrdered(unsigned short predicate) {
2388 switch (predicate) {
2389 default: return false;
2390 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2391 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2392 case FCmpInst::FCMP_ORD: return true;
2393 }
2394}
2395
2396bool CmpInst::isUnordered(unsigned short predicate) {
2397 switch (predicate) {
2398 default: return false;
2399 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2400 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2401 case FCmpInst::FCMP_UNO: return true;
2402 }
2403}
2404
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002405//===----------------------------------------------------------------------===//
2406// SwitchInst Implementation
2407//===----------------------------------------------------------------------===//
2408
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002409void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002410 assert(Value && Default);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002411 ReservedSpace = 2+NumCases*2;
2412 NumOperands = 2;
2413 OperandList = new Use[ReservedSpace];
2414
2415 OperandList[0].init(Value, this);
2416 OperandList[1].init(Default, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002417}
2418
Chris Lattner2195fc42007-02-24 00:55:48 +00002419/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2420/// switch on and a default destination. The number of additional cases can
2421/// be specified here to make memory allocation more efficient. This
2422/// constructor can also autoinsert before another instruction.
2423SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2424 Instruction *InsertBefore)
2425 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertBefore) {
2426 init(Value, Default, NumCases);
2427}
2428
2429/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2430/// switch on and a default destination. The number of additional cases can
2431/// be specified here to make memory allocation more efficient. This
2432/// constructor also autoinserts at the end of the specified BasicBlock.
2433SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2434 BasicBlock *InsertAtEnd)
2435 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertAtEnd) {
2436 init(Value, Default, NumCases);
2437}
2438
Misha Brukmanb1c93172005-04-21 23:48:37 +00002439SwitchInst::SwitchInst(const SwitchInst &SI)
Chris Lattner2195fc42007-02-24 00:55:48 +00002440 : TerminatorInst(Type::VoidTy, Instruction::Switch,
2441 new Use[SI.getNumOperands()], SI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002442 Use *OL = OperandList, *InOL = SI.OperandList;
2443 for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
2444 OL[i].init(InOL[i], this);
2445 OL[i+1].init(InOL[i+1], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002446 }
2447}
2448
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002449SwitchInst::~SwitchInst() {
2450 delete [] OperandList;
2451}
2452
2453
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002454/// addCase - Add an entry to the switch instruction...
2455///
Chris Lattner47ac1872005-02-24 05:32:09 +00002456void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002457 unsigned OpNo = NumOperands;
2458 if (OpNo+2 > ReservedSpace)
2459 resizeOperands(0); // Get more space!
2460 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002461 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002462 NumOperands = OpNo+2;
2463 OperandList[OpNo].init(OnVal, this);
2464 OperandList[OpNo+1].init(Dest, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002465}
2466
2467/// removeCase - This method removes the specified successor from the switch
2468/// instruction. Note that this cannot be used to remove the default
2469/// destination (successor #0).
2470///
2471void SwitchInst::removeCase(unsigned idx) {
2472 assert(idx != 0 && "Cannot remove the default case!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002473 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
2474
2475 unsigned NumOps = getNumOperands();
2476 Use *OL = OperandList;
2477
2478 // Move everything after this operand down.
2479 //
2480 // FIXME: we could just swap with the end of the list, then erase. However,
2481 // client might not expect this to happen. The code as it is thrashes the
2482 // use/def lists, which is kinda lame.
2483 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
2484 OL[i-2] = OL[i];
2485 OL[i-2+1] = OL[i+1];
2486 }
2487
2488 // Nuke the last value.
2489 OL[NumOps-2].set(0);
2490 OL[NumOps-2+1].set(0);
2491 NumOperands = NumOps-2;
2492}
2493
2494/// resizeOperands - resize operands - This adjusts the length of the operands
2495/// list according to the following behavior:
2496/// 1. If NumOps == 0, grow the operand list in response to a push_back style
2497/// of operation. This grows the number of ops by 1.5 times.
2498/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
2499/// 3. If NumOps == NumOperands, trim the reserved space.
2500///
2501void SwitchInst::resizeOperands(unsigned NumOps) {
2502 if (NumOps == 0) {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002503 NumOps = getNumOperands()/2*6;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002504 } else if (NumOps*2 > NumOperands) {
2505 // No resize needed.
2506 if (ReservedSpace >= NumOps) return;
2507 } else if (NumOps == NumOperands) {
2508 if (ReservedSpace == NumOps) return;
2509 } else {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002510 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002511 }
2512
2513 ReservedSpace = NumOps;
2514 Use *NewOps = new Use[NumOps];
2515 Use *OldOps = OperandList;
2516 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2517 NewOps[i].init(OldOps[i], this);
2518 OldOps[i].set(0);
2519 }
2520 delete [] OldOps;
2521 OperandList = NewOps;
2522}
2523
2524
2525BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
2526 return getSuccessor(idx);
2527}
2528unsigned SwitchInst::getNumSuccessorsV() const {
2529 return getNumSuccessors();
2530}
2531void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
2532 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002533}
Chris Lattnerf22be932004-10-15 23:52:53 +00002534
2535
2536// Define these methods here so vtables don't get emitted into every translation
2537// unit that uses these classes.
2538
2539GetElementPtrInst *GetElementPtrInst::clone() const {
2540 return new GetElementPtrInst(*this);
2541}
2542
2543BinaryOperator *BinaryOperator::clone() const {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002544 return create(getOpcode(), Ops[0], Ops[1]);
Chris Lattnerf22be932004-10-15 23:52:53 +00002545}
2546
Chris Lattner0b490b02007-08-24 20:48:18 +00002547FCmpInst* FCmpInst::clone() const {
2548 return new FCmpInst(getPredicate(), Ops[0], Ops[1]);
2549}
2550ICmpInst* ICmpInst::clone() const {
2551 return new ICmpInst(getPredicate(), Ops[0], Ops[1]);
Reid Spencerd9436b62006-11-20 01:22:35 +00002552}
2553
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002554MallocInst *MallocInst::clone() const { return new MallocInst(*this); }
2555AllocaInst *AllocaInst::clone() const { return new AllocaInst(*this); }
2556FreeInst *FreeInst::clone() const { return new FreeInst(getOperand(0)); }
2557LoadInst *LoadInst::clone() const { return new LoadInst(*this); }
2558StoreInst *StoreInst::clone() const { return new StoreInst(*this); }
2559CastInst *TruncInst::clone() const { return new TruncInst(*this); }
2560CastInst *ZExtInst::clone() const { return new ZExtInst(*this); }
2561CastInst *SExtInst::clone() const { return new SExtInst(*this); }
2562CastInst *FPTruncInst::clone() const { return new FPTruncInst(*this); }
2563CastInst *FPExtInst::clone() const { return new FPExtInst(*this); }
2564CastInst *UIToFPInst::clone() const { return new UIToFPInst(*this); }
2565CastInst *SIToFPInst::clone() const { return new SIToFPInst(*this); }
2566CastInst *FPToUIInst::clone() const { return new FPToUIInst(*this); }
2567CastInst *FPToSIInst::clone() const { return new FPToSIInst(*this); }
2568CastInst *PtrToIntInst::clone() const { return new PtrToIntInst(*this); }
2569CastInst *IntToPtrInst::clone() const { return new IntToPtrInst(*this); }
2570CastInst *BitCastInst::clone() const { return new BitCastInst(*this); }
2571CallInst *CallInst::clone() const { return new CallInst(*this); }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002572SelectInst *SelectInst::clone() const { return new SelectInst(*this); }
2573VAArgInst *VAArgInst::clone() const { return new VAArgInst(*this); }
2574
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002575ExtractElementInst *ExtractElementInst::clone() const {
2576 return new ExtractElementInst(*this);
2577}
2578InsertElementInst *InsertElementInst::clone() const {
2579 return new InsertElementInst(*this);
2580}
2581ShuffleVectorInst *ShuffleVectorInst::clone() const {
2582 return new ShuffleVectorInst(*this);
2583}
Chris Lattnerf22be932004-10-15 23:52:53 +00002584PHINode *PHINode::clone() const { return new PHINode(*this); }
2585ReturnInst *ReturnInst::clone() const { return new ReturnInst(*this); }
2586BranchInst *BranchInst::clone() const { return new BranchInst(*this); }
2587SwitchInst *SwitchInst::clone() const { return new SwitchInst(*this); }
2588InvokeInst *InvokeInst::clone() const { return new InvokeInst(*this); }
2589UnwindInst *UnwindInst::clone() const { return new UnwindInst(); }
Chris Lattner5e0b9f22004-10-16 18:08:06 +00002590UnreachableInst *UnreachableInst::clone() const { return new UnreachableInst();}