blob: bc4281cb465bf10c239e6070cd8a0d80a1bb30c7 [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"
20#include "llvm/Support/CallSite.h"
21using namespace llvm;
22
Chris Lattnerf7b6d312005-05-06 20:26:43 +000023unsigned CallSite::getCallingConv() const {
24 if (CallInst *CI = dyn_cast<CallInst>(I))
25 return CI->getCallingConv();
26 else
27 return cast<InvokeInst>(I)->getCallingConv();
28}
29void CallSite::setCallingConv(unsigned CC) {
30 if (CallInst *CI = dyn_cast<CallInst>(I))
31 CI->setCallingConv(CC);
32 else
33 cast<InvokeInst>(I)->setCallingConv(CC);
34}
35
36
Chris Lattner1c12a882006-06-21 16:53:47 +000037
38
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000039//===----------------------------------------------------------------------===//
Chris Lattnerafdb3de2005-01-29 00:35:16 +000040// TerminatorInst Class
41//===----------------------------------------------------------------------===//
42
43TerminatorInst::TerminatorInst(Instruction::TermOps iType,
Misha Brukmanb1c93172005-04-21 23:48:37 +000044 Use *Ops, unsigned NumOps, Instruction *IB)
Chris Lattnerafdb3de2005-01-29 00:35:16 +000045 : Instruction(Type::VoidTy, iType, Ops, NumOps, "", IB) {
46}
47
48TerminatorInst::TerminatorInst(Instruction::TermOps iType,
49 Use *Ops, unsigned NumOps, BasicBlock *IAE)
50 : Instruction(Type::VoidTy, iType, Ops, NumOps, "", IAE) {
51}
52
Chris Lattner1c12a882006-06-21 16:53:47 +000053// Out of line virtual method, so the vtable, etc has a home.
54TerminatorInst::~TerminatorInst() {
55}
56
57// Out of line virtual method, so the vtable, etc has a home.
58UnaryInstruction::~UnaryInstruction() {
59}
Chris Lattnerafdb3de2005-01-29 00:35:16 +000060
61
62//===----------------------------------------------------------------------===//
63// PHINode Class
64//===----------------------------------------------------------------------===//
65
66PHINode::PHINode(const PHINode &PN)
67 : Instruction(PN.getType(), Instruction::PHI,
68 new Use[PN.getNumOperands()], PN.getNumOperands()),
69 ReservedSpace(PN.getNumOperands()) {
70 Use *OL = OperandList;
71 for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) {
72 OL[i].init(PN.getOperand(i), this);
73 OL[i+1].init(PN.getOperand(i+1), this);
74 }
75}
76
77PHINode::~PHINode() {
78 delete [] OperandList;
79}
80
81// removeIncomingValue - Remove an incoming value. This is useful if a
82// predecessor basic block is deleted.
83Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
84 unsigned NumOps = getNumOperands();
85 Use *OL = OperandList;
86 assert(Idx*2 < NumOps && "BB not in PHI node!");
87 Value *Removed = OL[Idx*2];
88
89 // Move everything after this operand down.
90 //
91 // FIXME: we could just swap with the end of the list, then erase. However,
92 // client might not expect this to happen. The code as it is thrashes the
93 // use/def lists, which is kinda lame.
94 for (unsigned i = (Idx+1)*2; i != NumOps; i += 2) {
95 OL[i-2] = OL[i];
96 OL[i-2+1] = OL[i+1];
97 }
98
99 // Nuke the last value.
100 OL[NumOps-2].set(0);
101 OL[NumOps-2+1].set(0);
102 NumOperands = NumOps-2;
103
104 // If the PHI node is dead, because it has zero entries, nuke it now.
105 if (NumOps == 2 && DeletePHIIfEmpty) {
106 // If anyone is using this PHI, make them use a dummy value instead...
107 replaceAllUsesWith(UndefValue::get(getType()));
108 eraseFromParent();
109 }
110 return Removed;
111}
112
113/// resizeOperands - resize operands - This adjusts the length of the operands
114/// list according to the following behavior:
115/// 1. If NumOps == 0, grow the operand list in response to a push_back style
116/// of operation. This grows the number of ops by 1.5 times.
117/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
118/// 3. If NumOps == NumOperands, trim the reserved space.
119///
120void PHINode::resizeOperands(unsigned NumOps) {
121 if (NumOps == 0) {
122 NumOps = (getNumOperands())*3/2;
123 if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common.
124 } else if (NumOps*2 > NumOperands) {
125 // No resize needed.
126 if (ReservedSpace >= NumOps) return;
127 } else if (NumOps == NumOperands) {
128 if (ReservedSpace == NumOps) return;
129 } else {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000130 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000131 }
132
133 ReservedSpace = NumOps;
134 Use *NewOps = new Use[NumOps];
135 Use *OldOps = OperandList;
136 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
137 NewOps[i].init(OldOps[i], this);
138 OldOps[i].set(0);
139 }
140 delete [] OldOps;
141 OperandList = NewOps;
142}
143
Nate Begemanb3923212005-08-04 23:24:19 +0000144/// hasConstantValue - If the specified PHI node always merges together the same
145/// value, return the value, otherwise return null.
146///
Chris Lattner1d8b2482005-08-05 00:49:06 +0000147Value *PHINode::hasConstantValue(bool AllowNonDominatingInstruction) const {
Nate Begemanb3923212005-08-04 23:24:19 +0000148 // If the PHI node only has one incoming value, eliminate the PHI node...
149 if (getNumIncomingValues() == 1)
Chris Lattner6e709c12005-08-05 15:37:31 +0000150 if (getIncomingValue(0) != this) // not X = phi X
151 return getIncomingValue(0);
152 else
153 return UndefValue::get(getType()); // Self cycle is dead.
154
Nate Begemanb3923212005-08-04 23:24:19 +0000155 // Otherwise if all of the incoming values are the same for the PHI, replace
156 // the PHI node with the incoming value.
157 //
158 Value *InVal = 0;
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000159 bool HasUndefInput = false;
Nate Begemanb3923212005-08-04 23:24:19 +0000160 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i)
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000161 if (isa<UndefValue>(getIncomingValue(i)))
162 HasUndefInput = true;
163 else if (getIncomingValue(i) != this) // Not the PHI node itself...
Nate Begemanb3923212005-08-04 23:24:19 +0000164 if (InVal && getIncomingValue(i) != InVal)
165 return 0; // Not the same, bail out.
166 else
167 InVal = getIncomingValue(i);
168
169 // The only case that could cause InVal to be null is if we have a PHI node
170 // that only has entries for itself. In this case, there is no entry into the
171 // loop, so kill the PHI.
172 //
173 if (InVal == 0) InVal = UndefValue::get(getType());
174
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000175 // If we have a PHI node like phi(X, undef, X), where X is defined by some
176 // instruction, we cannot always return X as the result of the PHI node. Only
177 // do this if X is not an instruction (thus it must dominate the PHI block),
178 // or if the client is prepared to deal with this possibility.
179 if (HasUndefInput && !AllowNonDominatingInstruction)
180 if (Instruction *IV = dyn_cast<Instruction>(InVal))
181 // If it's in the entry block, it dominates everything.
Chris Lattner37774af2005-08-05 01:03:27 +0000182 if (IV->getParent() != &IV->getParent()->getParent()->front() ||
183 isa<InvokeInst>(IV))
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000184 return 0; // Cannot guarantee that InVal dominates this PHINode.
185
Nate Begemanb3923212005-08-04 23:24:19 +0000186 // All of the incoming values are the same, return the value now.
187 return InVal;
188}
189
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000190
191//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000192// CallInst Implementation
193//===----------------------------------------------------------------------===//
194
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000195CallInst::~CallInst() {
196 delete [] OperandList;
197}
198
Chris Lattner054ba2c2007-02-13 00:58:44 +0000199void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) {
200 NumOperands = NumParams+1;
201 Use *OL = OperandList = new Use[NumParams+1];
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000202 OL[0].init(Func, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000203
Misha Brukmanb1c93172005-04-21 23:48:37 +0000204 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000205 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000206 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000207
Chris Lattner054ba2c2007-02-13 00:58:44 +0000208 assert((NumParams == FTy->getNumParams() ||
209 (FTy->isVarArg() && NumParams > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000210 "Calling a function with bad signature!");
Chris Lattner054ba2c2007-02-13 00:58:44 +0000211 for (unsigned i = 0; i != NumParams; ++i) {
Chris Lattner667a0562006-05-03 00:48:22 +0000212 assert((i >= FTy->getNumParams() ||
213 FTy->getParamType(i) == Params[i]->getType()) &&
214 "Calling a function with a bad signature!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000215 OL[i+1].init(Params[i], this);
Chris Lattner667a0562006-05-03 00:48:22 +0000216 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000217}
218
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000219void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
220 NumOperands = 3;
221 Use *OL = OperandList = new Use[3];
222 OL[0].init(Func, this);
223 OL[1].init(Actual1, this);
224 OL[2].init(Actual2, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000225
226 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000227 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000228 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000229
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000230 assert((FTy->getNumParams() == 2 ||
Chris Lattner667a0562006-05-03 00:48:22 +0000231 (FTy->isVarArg() && FTy->getNumParams() < 2)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000232 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000233 assert((0 >= FTy->getNumParams() ||
234 FTy->getParamType(0) == Actual1->getType()) &&
235 "Calling a function with a bad signature!");
236 assert((1 >= FTy->getNumParams() ||
237 FTy->getParamType(1) == Actual2->getType()) &&
238 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000239}
240
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000241void CallInst::init(Value *Func, Value *Actual) {
242 NumOperands = 2;
243 Use *OL = OperandList = new Use[2];
244 OL[0].init(Func, this);
245 OL[1].init(Actual, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000246
247 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000248 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000249 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000250
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000251 assert((FTy->getNumParams() == 1 ||
252 (FTy->isVarArg() && FTy->getNumParams() == 0)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000253 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000254 assert((0 == FTy->getNumParams() ||
255 FTy->getParamType(0) == Actual->getType()) &&
256 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000257}
258
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000259void CallInst::init(Value *Func) {
260 NumOperands = 1;
261 Use *OL = OperandList = new Use[1];
262 OL[0].init(Func, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000263
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000264 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000265 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000266 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000267
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000268 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000269}
270
Misha Brukmanb1c93172005-04-21 23:48:37 +0000271CallInst::CallInst(Value *Func, const std::vector<Value*> &Params,
272 const std::string &Name, Instruction *InsertBefore)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000273 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
274 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000275 Instruction::Call, 0, 0, Name, InsertBefore) {
Chris Lattner054ba2c2007-02-13 00:58:44 +0000276 init(Func, &Params[0], Params.size());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000277}
278
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000279CallInst::CallInst(Value *Func, Value* const *Args, unsigned NumArgs,
Misha Brukmanb1c93172005-04-21 23:48:37 +0000280 const std::string &Name, BasicBlock *InsertAtEnd)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000281 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
282 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000283 Instruction::Call, 0, 0, Name, InsertAtEnd) {
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000284 init(Func, Args, NumArgs);
285}
286CallInst::CallInst(Value *Func, Value* const *Args, unsigned NumArgs,
287 const std::string &Name, Instruction *InsertBefore)
288: Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
289 ->getElementType())->getReturnType(),
290 Instruction::Call, 0, 0, Name, InsertBefore) {
291 init(Func, Args, NumArgs);
292}
293
294CallInst::CallInst(Value *Func, const std::vector<Value*> &Params,
295 const std::string &Name, BasicBlock *InsertAtEnd)
296: Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
297 ->getElementType())->getReturnType(),
298 Instruction::Call, 0, 0, Name, InsertAtEnd) {
Chris Lattner054ba2c2007-02-13 00:58:44 +0000299 init(Func, &Params[0], Params.size());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000300}
301
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000302
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000303CallInst::CallInst(Value *Func, Value *Actual1, Value *Actual2,
304 const std::string &Name, Instruction *InsertBefore)
305 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
306 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000307 Instruction::Call, 0, 0, Name, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000308 init(Func, Actual1, Actual2);
309}
310
311CallInst::CallInst(Value *Func, Value *Actual1, Value *Actual2,
312 const std::string &Name, BasicBlock *InsertAtEnd)
313 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
314 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000315 Instruction::Call, 0, 0, Name, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000316 init(Func, Actual1, Actual2);
317}
318
319CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
320 Instruction *InsertBefore)
321 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
322 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000323 Instruction::Call, 0, 0, Name, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000324 init(Func, Actual);
325}
326
327CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
328 BasicBlock *InsertAtEnd)
329 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
330 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000331 Instruction::Call, 0, 0, Name, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000332 init(Func, Actual);
333}
334
335CallInst::CallInst(Value *Func, const std::string &Name,
336 Instruction *InsertBefore)
337 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
338 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000339 Instruction::Call, 0, 0, Name, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000340 init(Func);
341}
342
343CallInst::CallInst(Value *Func, const std::string &Name,
344 BasicBlock *InsertAtEnd)
345 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
346 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000347 Instruction::Call, 0, 0, Name, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000348 init(Func);
349}
350
Misha Brukmanb1c93172005-04-21 23:48:37 +0000351CallInst::CallInst(const CallInst &CI)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000352 : Instruction(CI.getType(), Instruction::Call, new Use[CI.getNumOperands()],
353 CI.getNumOperands()) {
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000354 SubclassData = CI.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000355 Use *OL = OperandList;
356 Use *InOL = CI.OperandList;
357 for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i)
358 OL[i].init(InOL[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000359}
360
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000361
362//===----------------------------------------------------------------------===//
363// InvokeInst Implementation
364//===----------------------------------------------------------------------===//
365
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000366InvokeInst::~InvokeInst() {
367 delete [] OperandList;
368}
369
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000370void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000371 Value* const *Args, unsigned NumArgs) {
372 NumOperands = 3+NumArgs;
373 Use *OL = OperandList = new Use[3+NumArgs];
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000374 OL[0].init(Fn, this);
375 OL[1].init(IfNormal, this);
376 OL[2].init(IfException, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000377 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000378 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000379 FTy = FTy; // silence warning.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000380
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000381 assert((NumArgs == FTy->getNumParams()) ||
382 (FTy->isVarArg() && NumArgs > FTy->getNumParams()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000383 "Calling a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000384
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000385 for (unsigned i = 0, e = NumArgs; i != e; i++) {
Chris Lattner667a0562006-05-03 00:48:22 +0000386 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000387 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000388 "Invoking a function with a bad signature!");
389
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000390 OL[i+3].init(Args[i], this);
Chris Lattner667a0562006-05-03 00:48:22 +0000391 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000392}
393
394InvokeInst::InvokeInst(Value *Fn, BasicBlock *IfNormal,
395 BasicBlock *IfException,
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000396 Value* const *Args, unsigned NumArgs,
397 const std::string &Name, Instruction *InsertBefore)
398 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Fn->getType())
399 ->getElementType())->getReturnType(),
400 Instruction::Invoke, 0, 0, Name, InsertBefore) {
401 init(Fn, IfNormal, IfException, Args, NumArgs);
402}
403
404InvokeInst::InvokeInst(Value *Fn, BasicBlock *IfNormal,
405 BasicBlock *IfException,
406 Value* const *Args, unsigned NumArgs,
407 const std::string &Name, BasicBlock *InsertAtEnd)
408 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Fn->getType())
409 ->getElementType())->getReturnType(),
410 Instruction::Invoke, 0, 0, Name, InsertAtEnd) {
411 init(Fn, IfNormal, IfException, Args, NumArgs);
412}
413
414InvokeInst::InvokeInst(Value *Fn, BasicBlock *IfNormal,
415 BasicBlock *IfException,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000416 const std::vector<Value*> &Params,
417 const std::string &Name, Instruction *InsertBefore)
418 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Fn->getType())
419 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000420 Instruction::Invoke, 0, 0, Name, InsertBefore) {
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000421 init(Fn, IfNormal, IfException, &Params[0], Params.size());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000422}
423
424InvokeInst::InvokeInst(Value *Fn, BasicBlock *IfNormal,
425 BasicBlock *IfException,
426 const std::vector<Value*> &Params,
427 const std::string &Name, BasicBlock *InsertAtEnd)
428 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Fn->getType())
429 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000430 Instruction::Invoke, 0, 0, Name, InsertAtEnd) {
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000431 init(Fn, IfNormal, IfException, &Params[0], Params.size());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000432}
433
Misha Brukmanb1c93172005-04-21 23:48:37 +0000434InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000435 : TerminatorInst(II.getType(), Instruction::Invoke,
436 new Use[II.getNumOperands()], II.getNumOperands()) {
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000437 SubclassData = II.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000438 Use *OL = OperandList, *InOL = II.OperandList;
439 for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
440 OL[i].init(InOL[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000441}
442
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000443BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
444 return getSuccessor(idx);
445}
446unsigned InvokeInst::getNumSuccessorsV() const {
447 return getNumSuccessors();
448}
449void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
450 return setSuccessor(idx, B);
451}
452
453
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000454//===----------------------------------------------------------------------===//
455// ReturnInst Implementation
456//===----------------------------------------------------------------------===//
457
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000458void ReturnInst::init(Value *retVal) {
459 if (retVal && retVal->getType() != Type::VoidTy) {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000460 assert(!isa<BasicBlock>(retVal) &&
Alkis Evlogimenos531e9012004-11-17 21:02:25 +0000461 "Cannot return basic block. Probably using the incorrect ctor");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000462 NumOperands = 1;
463 RetVal.init(retVal, this);
Alkis Evlogimenos531e9012004-11-17 21:02:25 +0000464 }
465}
466
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000467unsigned ReturnInst::getNumSuccessorsV() const {
468 return getNumSuccessors();
469}
470
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000471// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
472// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000473void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000474 assert(0 && "ReturnInst has no successors!");
475}
476
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000477BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
478 assert(0 && "ReturnInst has no successors!");
479 abort();
480 return 0;
481}
482
483
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000484//===----------------------------------------------------------------------===//
485// UnwindInst Implementation
486//===----------------------------------------------------------------------===//
487
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000488unsigned UnwindInst::getNumSuccessorsV() const {
489 return getNumSuccessors();
490}
491
492void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000493 assert(0 && "UnwindInst has no successors!");
494}
495
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000496BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
497 assert(0 && "UnwindInst has no successors!");
498 abort();
499 return 0;
500}
501
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000502//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000503// UnreachableInst Implementation
504//===----------------------------------------------------------------------===//
505
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000506unsigned UnreachableInst::getNumSuccessorsV() const {
507 return getNumSuccessors();
508}
509
510void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
511 assert(0 && "UnwindInst has no successors!");
512}
513
514BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
515 assert(0 && "UnwindInst has no successors!");
516 abort();
517 return 0;
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000518}
519
520//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000521// BranchInst Implementation
522//===----------------------------------------------------------------------===//
523
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000524void BranchInst::AssertOK() {
525 if (isConditional())
Reid Spencer542964f2007-01-11 18:21:29 +0000526 assert(getCondition()->getType() == Type::Int1Ty &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000527 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000528}
529
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000530BranchInst::BranchInst(const BranchInst &BI) :
531 TerminatorInst(Instruction::Br, Ops, BI.getNumOperands()) {
532 OperandList[0].init(BI.getOperand(0), this);
533 if (BI.getNumOperands() != 1) {
534 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
535 OperandList[1].init(BI.getOperand(1), this);
536 OperandList[2].init(BI.getOperand(2), this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000537 }
538}
539
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000540BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
541 return getSuccessor(idx);
542}
543unsigned BranchInst::getNumSuccessorsV() const {
544 return getNumSuccessors();
545}
546void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
547 setSuccessor(idx, B);
548}
549
550
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000551//===----------------------------------------------------------------------===//
552// AllocationInst Implementation
553//===----------------------------------------------------------------------===//
554
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000555static Value *getAISize(Value *Amt) {
556 if (!Amt)
Reid Spencer8d9336d2006-12-31 05:26:44 +0000557 Amt = ConstantInt::get(Type::Int32Ty, 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000558 else {
559 assert(!isa<BasicBlock>(Amt) &&
560 "Passed basic block into allocation size parameter! Ue other ctor");
Reid Spencer8d9336d2006-12-31 05:26:44 +0000561 assert(Amt->getType() == Type::Int32Ty &&
Reid Spencer7e16e232007-01-26 06:30:34 +0000562 "Malloc/Allocation array size is not a 32-bit integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000563 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000564 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000565}
566
Misha Brukmanb1c93172005-04-21 23:48:37 +0000567AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000568 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000569 Instruction *InsertBefore)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000570 : UnaryInstruction(PointerType::get(Ty), iTy, getAISize(ArraySize),
Nate Begeman848622f2005-11-05 09:21:28 +0000571 Name, InsertBefore), Alignment(Align) {
Chris Lattner79b8c792005-11-05 21:57:54 +0000572 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000573 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000574}
575
Misha Brukmanb1c93172005-04-21 23:48:37 +0000576AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000577 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000578 BasicBlock *InsertAtEnd)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000579 : UnaryInstruction(PointerType::get(Ty), iTy, getAISize(ArraySize),
Nate Begeman848622f2005-11-05 09:21:28 +0000580 Name, InsertAtEnd), Alignment(Align) {
Chris Lattner79b8c792005-11-05 21:57:54 +0000581 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000582 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000583}
584
Chris Lattner1c12a882006-06-21 16:53:47 +0000585// Out of line virtual method, so the vtable, etc has a home.
586AllocationInst::~AllocationInst() {
587}
588
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000589bool AllocationInst::isArrayAllocation() const {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000590 if (ConstantInt *CUI = dyn_cast<ConstantInt>(getOperand(0)))
591 return CUI->getZExtValue() != 1;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000592 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000593}
594
595const Type *AllocationInst::getAllocatedType() const {
596 return getType()->getElementType();
597}
598
599AllocaInst::AllocaInst(const AllocaInst &AI)
600 : AllocationInst(AI.getType()->getElementType(), (Value*)AI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000601 Instruction::Alloca, AI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000602}
603
604MallocInst::MallocInst(const MallocInst &MI)
605 : AllocationInst(MI.getType()->getElementType(), (Value*)MI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000606 Instruction::Malloc, MI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000607}
608
609//===----------------------------------------------------------------------===//
610// FreeInst Implementation
611//===----------------------------------------------------------------------===//
612
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000613void FreeInst::AssertOK() {
614 assert(isa<PointerType>(getOperand(0)->getType()) &&
615 "Can not free something of nonpointer type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000616}
617
618FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000619 : UnaryInstruction(Type::VoidTy, Free, Ptr, "", InsertBefore) {
620 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000621}
622
623FreeInst::FreeInst(Value *Ptr, BasicBlock *InsertAtEnd)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000624 : UnaryInstruction(Type::VoidTy, Free, Ptr, "", InsertAtEnd) {
625 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000626}
627
628
629//===----------------------------------------------------------------------===//
630// LoadInst Implementation
631//===----------------------------------------------------------------------===//
632
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000633void LoadInst::AssertOK() {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000634 assert(isa<PointerType>(getOperand(0)->getType()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000635 "Ptr must have pointer type.");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000636}
637
638LoadInst::LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000639 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattnerdf57a022005-02-05 01:38:38 +0000640 Load, Ptr, Name, InsertBef) {
641 setVolatile(false);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000642 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000643}
644
645LoadInst::LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000646 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattnerdf57a022005-02-05 01:38:38 +0000647 Load, Ptr, Name, InsertAE) {
648 setVolatile(false);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000649 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000650}
651
652LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
653 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000654 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattnerdf57a022005-02-05 01:38:38 +0000655 Load, Ptr, Name, InsertBef) {
656 setVolatile(isVolatile);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000657 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000658}
659
660LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
661 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000662 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattnerdf57a022005-02-05 01:38:38 +0000663 Load, Ptr, Name, InsertAE) {
664 setVolatile(isVolatile);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000665 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000666}
667
668
669//===----------------------------------------------------------------------===//
670// StoreInst Implementation
671//===----------------------------------------------------------------------===//
672
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000673void StoreInst::AssertOK() {
674 assert(isa<PointerType>(getOperand(1)->getType()) &&
675 "Ptr must have pointer type!");
676 assert(getOperand(0)->getType() ==
677 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +0000678 && "Ptr must be a pointer to Val type!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000679}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000680
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000681
682StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Chris Lattnerdf57a022005-02-05 01:38:38 +0000683 : Instruction(Type::VoidTy, Store, Ops, 2, "", InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000684 Ops[0].init(val, this);
685 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000686 setVolatile(false);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000687 AssertOK();
688}
689
690StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Chris Lattnerdf57a022005-02-05 01:38:38 +0000691 : Instruction(Type::VoidTy, Store, Ops, 2, "", InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000692 Ops[0].init(val, this);
693 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000694 setVolatile(false);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000695 AssertOK();
696}
697
Misha Brukmanb1c93172005-04-21 23:48:37 +0000698StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000699 Instruction *InsertBefore)
Chris Lattnerdf57a022005-02-05 01:38:38 +0000700 : Instruction(Type::VoidTy, Store, Ops, 2, "", InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000701 Ops[0].init(val, this);
702 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000703 setVolatile(isVolatile);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000704 AssertOK();
705}
706
Misha Brukmanb1c93172005-04-21 23:48:37 +0000707StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000708 BasicBlock *InsertAtEnd)
Chris Lattnerdf57a022005-02-05 01:38:38 +0000709 : Instruction(Type::VoidTy, Store, Ops, 2, "", InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000710 Ops[0].init(val, this);
711 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000712 setVolatile(isVolatile);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000713 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000714}
715
716//===----------------------------------------------------------------------===//
717// GetElementPtrInst Implementation
718//===----------------------------------------------------------------------===//
719
720// checkType - Simple wrapper function to give a better assertion failure
721// message on bad indexes for a gep instruction.
722//
723static inline const Type *checkType(const Type *Ty) {
Chris Lattner47a6e632006-05-14 18:34:36 +0000724 assert(Ty && "Invalid GetElementPtrInst indices for type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000725 return Ty;
726}
727
Chris Lattner79807c3d2007-01-31 19:47:18 +0000728void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx) {
729 NumOperands = 1+NumIdx;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000730 Use *OL = OperandList = new Use[NumOperands];
731 OL[0].init(Ptr, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000732
Chris Lattner79807c3d2007-01-31 19:47:18 +0000733 for (unsigned i = 0; i != NumIdx; ++i)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000734 OL[i+1].init(Idx[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000735}
736
737void GetElementPtrInst::init(Value *Ptr, Value *Idx0, Value *Idx1) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000738 NumOperands = 3;
739 Use *OL = OperandList = new Use[3];
740 OL[0].init(Ptr, this);
741 OL[1].init(Idx0, this);
742 OL[2].init(Idx1, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000743}
744
Chris Lattner82981202005-05-03 05:43:30 +0000745void GetElementPtrInst::init(Value *Ptr, Value *Idx) {
746 NumOperands = 2;
747 Use *OL = OperandList = new Use[2];
748 OL[0].init(Ptr, this);
749 OL[1].init(Idx, this);
750}
751
Chris Lattner79807c3d2007-01-31 19:47:18 +0000752
753GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value* const *Idx,
754 unsigned NumIdx,
755 const std::string &Name, Instruction *InBe)
756: Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
Reid Spencerdee14b52007-01-31 22:30:26 +0000757 Idx, NumIdx, true))),
Chris Lattner79807c3d2007-01-31 19:47:18 +0000758 GetElementPtr, 0, 0, Name, InBe) {
759 init(Ptr, Idx, NumIdx);
760}
761
762GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value* const *Idx,
763 unsigned NumIdx,
764 const std::string &Name, BasicBlock *IAE)
765: Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
Reid Spencerdee14b52007-01-31 22:30:26 +0000766 Idx, NumIdx, true))),
Chris Lattner79807c3d2007-01-31 19:47:18 +0000767 GetElementPtr, 0, 0, Name, IAE) {
768 init(Ptr, Idx, NumIdx);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000769}
770
Chris Lattner82981202005-05-03 05:43:30 +0000771GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
772 const std::string &Name, Instruction *InBe)
Reid Spencerdee14b52007-01-31 22:30:26 +0000773 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
774 Idx))),
Chris Lattner82981202005-05-03 05:43:30 +0000775 GetElementPtr, 0, 0, Name, InBe) {
776 init(Ptr, Idx);
777}
778
779GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
780 const std::string &Name, BasicBlock *IAE)
Reid Spencerdee14b52007-01-31 22:30:26 +0000781 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
782 Idx))),
Chris Lattner82981202005-05-03 05:43:30 +0000783 GetElementPtr, 0, 0, Name, IAE) {
784 init(Ptr, Idx);
785}
786
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000787GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
788 const std::string &Name, Instruction *InBe)
789 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
790 Idx0, Idx1, true))),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000791 GetElementPtr, 0, 0, Name, InBe) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000792 init(Ptr, Idx0, Idx1);
793}
794
795GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
Misha Brukman96eb8782005-03-16 05:42:00 +0000796 const std::string &Name, BasicBlock *IAE)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000797 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
798 Idx0, Idx1, true))),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000799 GetElementPtr, 0, 0, Name, IAE) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000800 init(Ptr, Idx0, Idx1);
801}
802
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000803GetElementPtrInst::~GetElementPtrInst() {
804 delete[] OperandList;
805}
806
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000807// getIndexedType - Returns the type of the element that would be loaded with
808// a load instruction with the specified parameters.
809//
Misha Brukmanb1c93172005-04-21 23:48:37 +0000810// A null type is returned if the indices are invalid for the specified
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000811// pointer type.
812//
Misha Brukmanb1c93172005-04-21 23:48:37 +0000813const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
Chris Lattner302116a2007-01-31 04:40:28 +0000814 Value* const *Idxs,
815 unsigned NumIdx,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000816 bool AllowCompositeLeaf) {
817 if (!isa<PointerType>(Ptr)) return 0; // Type isn't a pointer type!
818
819 // Handle the special case of the empty set index set...
Chris Lattner302116a2007-01-31 04:40:28 +0000820 if (NumIdx == 0)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000821 if (AllowCompositeLeaf ||
822 cast<PointerType>(Ptr)->getElementType()->isFirstClassType())
823 return cast<PointerType>(Ptr)->getElementType();
824 else
825 return 0;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000826
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000827 unsigned CurIdx = 0;
828 while (const CompositeType *CT = dyn_cast<CompositeType>(Ptr)) {
Chris Lattner302116a2007-01-31 04:40:28 +0000829 if (NumIdx == CurIdx) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000830 if (AllowCompositeLeaf || CT->isFirstClassType()) return Ptr;
831 return 0; // Can't load a whole structure or array!?!?
832 }
833
Chris Lattner302116a2007-01-31 04:40:28 +0000834 Value *Index = Idxs[CurIdx++];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000835 if (isa<PointerType>(CT) && CurIdx != 1)
836 return 0; // Can only index into pointer types at the first index!
837 if (!CT->indexValid(Index)) return 0;
838 Ptr = CT->getTypeAtIndex(Index);
839
840 // If the new type forwards to another type, then it is in the middle
841 // of being refined to another type (and hence, may have dropped all
842 // references to what it was using before). So, use the new forwarded
843 // type.
844 if (const Type * Ty = Ptr->getForwardedType()) {
845 Ptr = Ty;
846 }
847 }
Chris Lattner302116a2007-01-31 04:40:28 +0000848 return CurIdx == NumIdx ? Ptr : 0;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000849}
850
Misha Brukmanb1c93172005-04-21 23:48:37 +0000851const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000852 Value *Idx0, Value *Idx1,
853 bool AllowCompositeLeaf) {
854 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
855 if (!PTy) return 0; // Type isn't a pointer type!
856
857 // Check the pointer index.
858 if (!PTy->indexValid(Idx0)) return 0;
859
860 const CompositeType *CT = dyn_cast<CompositeType>(PTy->getElementType());
861 if (!CT || !CT->indexValid(Idx1)) return 0;
862
863 const Type *ElTy = CT->getTypeAtIndex(Idx1);
864 if (AllowCompositeLeaf || ElTy->isFirstClassType())
865 return ElTy;
866 return 0;
867}
868
Chris Lattner82981202005-05-03 05:43:30 +0000869const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
870 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
871 if (!PTy) return 0; // Type isn't a pointer type!
872
873 // Check the pointer index.
874 if (!PTy->indexValid(Idx)) return 0;
875
Chris Lattnerc2233332005-05-03 16:44:45 +0000876 return PTy->getElementType();
Chris Lattner82981202005-05-03 05:43:30 +0000877}
878
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000879//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +0000880// ExtractElementInst Implementation
881//===----------------------------------------------------------------------===//
882
883ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000884 const std::string &Name,
885 Instruction *InsertBef)
Robert Bocchino23004482006-01-10 19:05:34 +0000886 : Instruction(cast<PackedType>(Val->getType())->getElementType(),
887 ExtractElement, Ops, 2, Name, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +0000888 assert(isValidOperands(Val, Index) &&
889 "Invalid extractelement instruction operands!");
Robert Bocchino23004482006-01-10 19:05:34 +0000890 Ops[0].init(Val, this);
891 Ops[1].init(Index, this);
892}
893
Chris Lattner65511ff2006-10-05 06:24:58 +0000894ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
895 const std::string &Name,
896 Instruction *InsertBef)
897 : Instruction(cast<PackedType>(Val->getType())->getElementType(),
898 ExtractElement, Ops, 2, Name, InsertBef) {
Reid Spencer8d9336d2006-12-31 05:26:44 +0000899 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +0000900 assert(isValidOperands(Val, Index) &&
901 "Invalid extractelement instruction operands!");
902 Ops[0].init(Val, this);
903 Ops[1].init(Index, this);
904}
905
906
Robert Bocchino23004482006-01-10 19:05:34 +0000907ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000908 const std::string &Name,
909 BasicBlock *InsertAE)
Robert Bocchino23004482006-01-10 19:05:34 +0000910 : Instruction(cast<PackedType>(Val->getType())->getElementType(),
911 ExtractElement, Ops, 2, Name, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +0000912 assert(isValidOperands(Val, Index) &&
913 "Invalid extractelement instruction operands!");
914
Robert Bocchino23004482006-01-10 19:05:34 +0000915 Ops[0].init(Val, this);
916 Ops[1].init(Index, this);
917}
918
Chris Lattner65511ff2006-10-05 06:24:58 +0000919ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
920 const std::string &Name,
921 BasicBlock *InsertAE)
922 : Instruction(cast<PackedType>(Val->getType())->getElementType(),
923 ExtractElement, Ops, 2, Name, InsertAE) {
Reid Spencer8d9336d2006-12-31 05:26:44 +0000924 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +0000925 assert(isValidOperands(Val, Index) &&
926 "Invalid extractelement instruction operands!");
927
928 Ops[0].init(Val, this);
929 Ops[1].init(Index, this);
930}
931
932
Chris Lattner54865b32006-04-08 04:05:48 +0000933bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Reid Spencer8d9336d2006-12-31 05:26:44 +0000934 if (!isa<PackedType>(Val->getType()) || Index->getType() != Type::Int32Ty)
Chris Lattner54865b32006-04-08 04:05:48 +0000935 return false;
936 return true;
937}
938
939
Robert Bocchino23004482006-01-10 19:05:34 +0000940//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +0000941// InsertElementInst Implementation
942//===----------------------------------------------------------------------===//
943
Chris Lattner0875d942006-04-14 22:20:32 +0000944InsertElementInst::InsertElementInst(const InsertElementInst &IE)
945 : Instruction(IE.getType(), InsertElement, Ops, 3) {
946 Ops[0].init(IE.Ops[0], this);
947 Ops[1].init(IE.Ops[1], this);
948 Ops[2].init(IE.Ops[2], this);
949}
Chris Lattner54865b32006-04-08 04:05:48 +0000950InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000951 const std::string &Name,
952 Instruction *InsertBef)
Chris Lattner54865b32006-04-08 04:05:48 +0000953 : Instruction(Vec->getType(), InsertElement, Ops, 3, Name, InsertBef) {
954 assert(isValidOperands(Vec, Elt, Index) &&
955 "Invalid insertelement instruction operands!");
956 Ops[0].init(Vec, this);
Robert Bocchinoca27f032006-01-17 20:07:22 +0000957 Ops[1].init(Elt, this);
958 Ops[2].init(Index, this);
959}
960
Chris Lattner65511ff2006-10-05 06:24:58 +0000961InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
962 const std::string &Name,
963 Instruction *InsertBef)
964 : Instruction(Vec->getType(), InsertElement, Ops, 3, Name, InsertBef) {
Reid Spencer8d9336d2006-12-31 05:26:44 +0000965 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +0000966 assert(isValidOperands(Vec, Elt, Index) &&
967 "Invalid insertelement instruction operands!");
968 Ops[0].init(Vec, this);
969 Ops[1].init(Elt, this);
970 Ops[2].init(Index, this);
971}
972
973
Chris Lattner54865b32006-04-08 04:05:48 +0000974InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000975 const std::string &Name,
976 BasicBlock *InsertAE)
Chris Lattner54865b32006-04-08 04:05:48 +0000977 : Instruction(Vec->getType(), InsertElement, Ops, 3, Name, InsertAE) {
978 assert(isValidOperands(Vec, Elt, Index) &&
979 "Invalid insertelement instruction operands!");
980
981 Ops[0].init(Vec, this);
Robert Bocchinoca27f032006-01-17 20:07:22 +0000982 Ops[1].init(Elt, this);
983 Ops[2].init(Index, this);
984}
985
Chris Lattner65511ff2006-10-05 06:24:58 +0000986InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
987 const std::string &Name,
988 BasicBlock *InsertAE)
989: Instruction(Vec->getType(), InsertElement, Ops, 3, Name, InsertAE) {
Reid Spencer8d9336d2006-12-31 05:26:44 +0000990 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +0000991 assert(isValidOperands(Vec, Elt, Index) &&
992 "Invalid insertelement instruction operands!");
993
994 Ops[0].init(Vec, this);
995 Ops[1].init(Elt, this);
996 Ops[2].init(Index, this);
997}
998
Chris Lattner54865b32006-04-08 04:05:48 +0000999bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1000 const Value *Index) {
1001 if (!isa<PackedType>(Vec->getType()))
1002 return false; // First operand of insertelement must be packed type.
1003
1004 if (Elt->getType() != cast<PackedType>(Vec->getType())->getElementType())
1005 return false;// Second operand of insertelement must be packed element type.
1006
Reid Spencer8d9336d2006-12-31 05:26:44 +00001007 if (Index->getType() != Type::Int32Ty)
Chris Lattner54865b32006-04-08 04:05:48 +00001008 return false; // Third operand of insertelement must be uint.
1009 return true;
1010}
1011
1012
Robert Bocchinoca27f032006-01-17 20:07:22 +00001013//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001014// ShuffleVectorInst Implementation
1015//===----------------------------------------------------------------------===//
1016
Chris Lattner0875d942006-04-14 22:20:32 +00001017ShuffleVectorInst::ShuffleVectorInst(const ShuffleVectorInst &SV)
1018 : Instruction(SV.getType(), ShuffleVector, Ops, 3) {
1019 Ops[0].init(SV.Ops[0], this);
1020 Ops[1].init(SV.Ops[1], this);
1021 Ops[2].init(SV.Ops[2], this);
1022}
1023
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001024ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1025 const std::string &Name,
1026 Instruction *InsertBefore)
1027 : Instruction(V1->getType(), ShuffleVector, Ops, 3, Name, InsertBefore) {
1028 assert(isValidOperands(V1, V2, Mask) &&
1029 "Invalid shuffle vector instruction operands!");
1030 Ops[0].init(V1, this);
1031 Ops[1].init(V2, this);
1032 Ops[2].init(Mask, this);
1033}
1034
1035ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1036 const std::string &Name,
1037 BasicBlock *InsertAtEnd)
1038 : Instruction(V1->getType(), ShuffleVector, Ops, 3, Name, InsertAtEnd) {
1039 assert(isValidOperands(V1, V2, Mask) &&
1040 "Invalid shuffle vector instruction operands!");
1041
1042 Ops[0].init(V1, this);
1043 Ops[1].init(V2, this);
1044 Ops[2].init(Mask, this);
1045}
1046
1047bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
1048 const Value *Mask) {
1049 if (!isa<PackedType>(V1->getType())) return false;
1050 if (V1->getType() != V2->getType()) return false;
1051 if (!isa<PackedType>(Mask->getType()) ||
Reid Spencer8d9336d2006-12-31 05:26:44 +00001052 cast<PackedType>(Mask->getType())->getElementType() != Type::Int32Ty ||
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001053 cast<PackedType>(Mask->getType())->getNumElements() !=
1054 cast<PackedType>(V1->getType())->getNumElements())
1055 return false;
1056 return true;
1057}
1058
1059
1060//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001061// BinaryOperator Class
1062//===----------------------------------------------------------------------===//
1063
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001064void BinaryOperator::init(BinaryOps iType)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001065{
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001066 Value *LHS = getOperand(0), *RHS = getOperand(1);
Chris Lattnerf14c76c2007-02-01 04:59:37 +00001067 LHS = LHS; RHS = RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001068 assert(LHS->getType() == RHS->getType() &&
1069 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001070#ifndef NDEBUG
1071 switch (iType) {
1072 case Add: case Sub:
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001073 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001074 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001075 "Arithmetic operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001076 assert((getType()->isInteger() || getType()->isFloatingPoint() ||
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001077 isa<PackedType>(getType())) &&
Brian Gaeke02209042004-08-20 06:00:58 +00001078 "Tried to create an arithmetic operation on a non-arithmetic type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001079 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001080 case UDiv:
1081 case SDiv:
1082 assert(getType() == LHS->getType() &&
1083 "Arithmetic operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001084 assert((getType()->isInteger() || (isa<PackedType>(getType()) &&
1085 cast<PackedType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001086 "Incorrect operand type (not integer) for S/UDIV");
1087 break;
1088 case FDiv:
1089 assert(getType() == LHS->getType() &&
1090 "Arithmetic operation should return same type as operands!");
1091 assert((getType()->isFloatingPoint() || (isa<PackedType>(getType()) &&
1092 cast<PackedType>(getType())->getElementType()->isFloatingPoint()))
1093 && "Incorrect operand type (not floating point) for FDIV");
1094 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001095 case URem:
1096 case SRem:
1097 assert(getType() == LHS->getType() &&
1098 "Arithmetic operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001099 assert((getType()->isInteger() || (isa<PackedType>(getType()) &&
1100 cast<PackedType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001101 "Incorrect operand type (not integer) for S/UREM");
1102 break;
1103 case FRem:
1104 assert(getType() == LHS->getType() &&
1105 "Arithmetic operation should return same type as operands!");
1106 assert((getType()->isFloatingPoint() || (isa<PackedType>(getType()) &&
1107 cast<PackedType>(getType())->getElementType()->isFloatingPoint()))
1108 && "Incorrect operand type (not floating point) for FREM");
1109 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001110 case Shl:
1111 case LShr:
1112 case AShr:
1113 assert(getType() == LHS->getType() &&
1114 "Shift operation should return same type as operands!");
1115 assert(getType()->isInteger() &&
1116 "Shift operation requires integer operands");
1117 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001118 case And: case Or:
1119 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001120 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001121 "Logical operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001122 assert((getType()->isInteger() ||
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001123 (isa<PackedType>(getType()) &&
Chris Lattner03c49532007-01-15 02:27:26 +00001124 cast<PackedType>(getType())->getElementType()->isInteger())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001125 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001126 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001127 default:
1128 break;
1129 }
1130#endif
1131}
1132
1133BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001134 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001135 Instruction *InsertBefore) {
1136 assert(S1->getType() == S2->getType() &&
1137 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001138 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001139}
1140
1141BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001142 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001143 BasicBlock *InsertAtEnd) {
1144 BinaryOperator *Res = create(Op, S1, S2, Name);
1145 InsertAtEnd->getInstList().push_back(Res);
1146 return Res;
1147}
1148
1149BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
1150 Instruction *InsertBefore) {
Reid Spencer2eadb532007-01-21 00:29:26 +00001151 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1152 return new BinaryOperator(Instruction::Sub,
1153 zero, Op,
1154 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001155}
1156
1157BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
1158 BasicBlock *InsertAtEnd) {
Reid Spencer2eadb532007-01-21 00:29:26 +00001159 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1160 return new BinaryOperator(Instruction::Sub,
1161 zero, Op,
1162 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001163}
1164
1165BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
1166 Instruction *InsertBefore) {
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001167 Constant *C;
1168 if (const PackedType *PTy = dyn_cast<PackedType>(Op->getType())) {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001169 C = ConstantInt::getAllOnesValue(PTy->getElementType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001170 C = ConstantPacked::get(std::vector<Constant*>(PTy->getNumElements(), C));
1171 } else {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001172 C = ConstantInt::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001173 }
1174
1175 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001176 Op->getType(), Name, InsertBefore);
1177}
1178
1179BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
1180 BasicBlock *InsertAtEnd) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001181 Constant *AllOnes;
1182 if (const PackedType *PTy = dyn_cast<PackedType>(Op->getType())) {
1183 // Create a vector of all ones values.
Zhou Sheng75b871f2007-01-11 12:24:14 +00001184 Constant *Elt = ConstantInt::getAllOnesValue(PTy->getElementType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001185 AllOnes =
1186 ConstantPacked::get(std::vector<Constant*>(PTy->getNumElements(), Elt));
1187 } else {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001188 AllOnes = ConstantInt::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001189 }
1190
1191 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001192 Op->getType(), Name, InsertAtEnd);
1193}
1194
1195
1196// isConstantAllOnes - Helper function for several functions below
1197static inline bool isConstantAllOnes(const Value *V) {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001198 return isa<ConstantInt>(V) &&cast<ConstantInt>(V)->isAllOnesValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001199}
1200
1201bool BinaryOperator::isNeg(const Value *V) {
1202 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1203 if (Bop->getOpcode() == Instruction::Sub)
Reid Spencer2eadb532007-01-21 00:29:26 +00001204 return Bop->getOperand(0) ==
1205 ConstantExpr::getZeroValueForNegationExpr(Bop->getType());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001206 return false;
1207}
1208
1209bool BinaryOperator::isNot(const Value *V) {
1210 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1211 return (Bop->getOpcode() == Instruction::Xor &&
1212 (isConstantAllOnes(Bop->getOperand(1)) ||
1213 isConstantAllOnes(Bop->getOperand(0))));
1214 return false;
1215}
1216
Chris Lattner2c7d1772005-04-24 07:28:37 +00001217Value *BinaryOperator::getNegArgument(Value *BinOp) {
1218 assert(isNeg(BinOp) && "getNegArgument from non-'neg' instruction!");
1219 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001220}
1221
Chris Lattner2c7d1772005-04-24 07:28:37 +00001222const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1223 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001224}
1225
Chris Lattner2c7d1772005-04-24 07:28:37 +00001226Value *BinaryOperator::getNotArgument(Value *BinOp) {
1227 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1228 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1229 Value *Op0 = BO->getOperand(0);
1230 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001231 if (isConstantAllOnes(Op0)) return Op1;
1232
1233 assert(isConstantAllOnes(Op1));
1234 return Op0;
1235}
1236
Chris Lattner2c7d1772005-04-24 07:28:37 +00001237const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1238 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001239}
1240
1241
1242// swapOperands - Exchange the two operands to this instruction. This
1243// instruction is safe to use on any binary instruction and does not
1244// modify the semantics of the instruction. If the instruction is
1245// order dependent (SetLT f.e.) the opcode is changed.
1246//
1247bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001248 if (!isCommutative())
1249 return true; // Can't commute operands
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001250 std::swap(Ops[0], Ops[1]);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001251 return false;
1252}
1253
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001254//===----------------------------------------------------------------------===//
1255// CastInst Class
1256//===----------------------------------------------------------------------===//
1257
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001258// Just determine if this cast only deals with integral->integral conversion.
1259bool CastInst::isIntegerCast() const {
1260 switch (getOpcode()) {
1261 default: return false;
1262 case Instruction::ZExt:
1263 case Instruction::SExt:
1264 case Instruction::Trunc:
1265 return true;
1266 case Instruction::BitCast:
Chris Lattner03c49532007-01-15 02:27:26 +00001267 return getOperand(0)->getType()->isInteger() && getType()->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001268 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001269}
1270
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001271bool CastInst::isLosslessCast() const {
1272 // Only BitCast can be lossless, exit fast if we're not BitCast
1273 if (getOpcode() != Instruction::BitCast)
1274 return false;
1275
1276 // Identity cast is always lossless
1277 const Type* SrcTy = getOperand(0)->getType();
1278 const Type* DstTy = getType();
1279 if (SrcTy == DstTy)
1280 return true;
1281
Reid Spencer8d9336d2006-12-31 05:26:44 +00001282 // Pointer to pointer is always lossless.
1283 if (isa<PointerType>(SrcTy))
1284 return isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001285 return false; // Other types have no identity values
1286}
1287
1288/// This function determines if the CastInst does not require any bits to be
1289/// changed in order to effect the cast. Essentially, it identifies cases where
1290/// no code gen is necessary for the cast, hence the name no-op cast. For
1291/// example, the following are all no-op casts:
1292/// # bitcast uint %X, int
1293/// # bitcast uint* %x, sbyte*
1294/// # bitcast packed< 2 x int > %x, packed< 4 x short>
1295/// # ptrtoint uint* %x, uint ; on 32-bit plaforms only
1296/// @brief Determine if a cast is a no-op.
1297bool CastInst::isNoopCast(const Type *IntPtrTy) const {
1298 switch (getOpcode()) {
1299 default:
1300 assert(!"Invalid CastOp");
1301 case Instruction::Trunc:
1302 case Instruction::ZExt:
1303 case Instruction::SExt:
1304 case Instruction::FPTrunc:
1305 case Instruction::FPExt:
1306 case Instruction::UIToFP:
1307 case Instruction::SIToFP:
1308 case Instruction::FPToUI:
1309 case Instruction::FPToSI:
1310 return false; // These always modify bits
1311 case Instruction::BitCast:
1312 return true; // BitCast never modifies bits.
1313 case Instruction::PtrToInt:
1314 return IntPtrTy->getPrimitiveSizeInBits() ==
1315 getType()->getPrimitiveSizeInBits();
1316 case Instruction::IntToPtr:
1317 return IntPtrTy->getPrimitiveSizeInBits() ==
1318 getOperand(0)->getType()->getPrimitiveSizeInBits();
1319 }
1320}
1321
1322/// This function determines if a pair of casts can be eliminated and what
1323/// opcode should be used in the elimination. This assumes that there are two
1324/// instructions like this:
1325/// * %F = firstOpcode SrcTy %x to MidTy
1326/// * %S = secondOpcode MidTy %F to DstTy
1327/// The function returns a resultOpcode so these two casts can be replaced with:
1328/// * %Replacement = resultOpcode %SrcTy %x to DstTy
1329/// If no such cast is permited, the function returns 0.
1330unsigned CastInst::isEliminableCastPair(
1331 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
1332 const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
1333{
1334 // Define the 144 possibilities for these two cast instructions. The values
1335 // in this matrix determine what to do in a given situation and select the
1336 // case in the switch below. The rows correspond to firstOp, the columns
1337 // correspond to secondOp. In looking at the table below, keep in mind
1338 // the following cast properties:
1339 //
1340 // Size Compare Source Destination
1341 // Operator Src ? Size Type Sign Type Sign
1342 // -------- ------------ ------------------- ---------------------
1343 // TRUNC > Integer Any Integral Any
1344 // ZEXT < Integral Unsigned Integer Any
1345 // SEXT < Integral Signed Integer Any
1346 // FPTOUI n/a FloatPt n/a Integral Unsigned
1347 // FPTOSI n/a FloatPt n/a Integral Signed
1348 // UITOFP n/a Integral Unsigned FloatPt n/a
1349 // SITOFP n/a Integral Signed FloatPt n/a
1350 // FPTRUNC > FloatPt n/a FloatPt n/a
1351 // FPEXT < FloatPt n/a FloatPt n/a
1352 // PTRTOINT n/a Pointer n/a Integral Unsigned
1353 // INTTOPTR n/a Integral Unsigned Pointer n/a
1354 // BITCONVERT = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00001355 //
1356 // NOTE: some transforms are safe, but we consider them to be non-profitable.
1357 // For example, we could merge "fptoui double to uint" + "zext uint to ulong",
1358 // into "fptoui double to ulong", but this loses information about the range
1359 // of the produced value (we no longer know the top-part is all zeros).
1360 // Further this conversion is often much more expensive for typical hardware,
1361 // and causes issues when building libgcc. We disallow fptosi+sext for the
1362 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001363 const unsigned numCastOps =
1364 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
1365 static const uint8_t CastResults[numCastOps][numCastOps] = {
1366 // T F F U S F F P I B -+
1367 // R Z S P P I I T P 2 N T |
1368 // U E E 2 2 2 2 R E I T C +- secondOp
1369 // N X X U S F F N X N 2 V |
1370 // C T T I I P P C T T P T -+
1371 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
1372 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
1373 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00001374 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
1375 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001376 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
1377 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
1378 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
1379 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
1380 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
1381 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
1382 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
1383 };
1384
1385 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
1386 [secondOp-Instruction::CastOpsBegin];
1387 switch (ElimCase) {
1388 case 0:
1389 // categorically disallowed
1390 return 0;
1391 case 1:
1392 // allowed, use first cast's opcode
1393 return firstOp;
1394 case 2:
1395 // allowed, use second cast's opcode
1396 return secondOp;
1397 case 3:
1398 // no-op cast in second op implies firstOp as long as the DestTy
1399 // is integer
Chris Lattner03c49532007-01-15 02:27:26 +00001400 if (DstTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001401 return firstOp;
1402 return 0;
1403 case 4:
1404 // no-op cast in second op implies firstOp as long as the DestTy
1405 // is floating point
1406 if (DstTy->isFloatingPoint())
1407 return firstOp;
1408 return 0;
1409 case 5:
1410 // no-op cast in first op implies secondOp as long as the SrcTy
1411 // is an integer
Chris Lattner03c49532007-01-15 02:27:26 +00001412 if (SrcTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001413 return secondOp;
1414 return 0;
1415 case 6:
1416 // no-op cast in first op implies secondOp as long as the SrcTy
1417 // is a floating point
1418 if (SrcTy->isFloatingPoint())
1419 return secondOp;
1420 return 0;
1421 case 7: {
1422 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
1423 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1424 unsigned MidSize = MidTy->getPrimitiveSizeInBits();
1425 if (MidSize >= PtrSize)
1426 return Instruction::BitCast;
1427 return 0;
1428 }
1429 case 8: {
1430 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
1431 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
1432 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
1433 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1434 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1435 if (SrcSize == DstSize)
1436 return Instruction::BitCast;
1437 else if (SrcSize < DstSize)
1438 return firstOp;
1439 return secondOp;
1440 }
1441 case 9: // zext, sext -> zext, because sext can't sign extend after zext
1442 return Instruction::ZExt;
1443 case 10:
1444 // fpext followed by ftrunc is allowed if the bit size returned to is
1445 // the same as the original, in which case its just a bitcast
1446 if (SrcTy == DstTy)
1447 return Instruction::BitCast;
1448 return 0; // If the types are not the same we can't eliminate it.
1449 case 11:
1450 // bitcast followed by ptrtoint is allowed as long as the bitcast
1451 // is a pointer to pointer cast.
1452 if (isa<PointerType>(SrcTy) && isa<PointerType>(MidTy))
1453 return secondOp;
1454 return 0;
1455 case 12:
1456 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
1457 if (isa<PointerType>(MidTy) && isa<PointerType>(DstTy))
1458 return firstOp;
1459 return 0;
1460 case 13: {
1461 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
1462 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1463 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1464 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1465 if (SrcSize <= PtrSize && SrcSize == DstSize)
1466 return Instruction::BitCast;
1467 return 0;
1468 }
1469 case 99:
1470 // cast combination can't happen (error in input). This is for all cases
1471 // where the MidTy is not the same for the two cast instructions.
1472 assert(!"Invalid Cast Combination");
1473 return 0;
1474 default:
1475 assert(!"Error in CastResults table!!!");
1476 return 0;
1477 }
1478 return 0;
1479}
1480
1481CastInst *CastInst::create(Instruction::CastOps op, Value *S, const Type *Ty,
1482 const std::string &Name, Instruction *InsertBefore) {
1483 // Construct and return the appropriate CastInst subclass
1484 switch (op) {
1485 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
1486 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
1487 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
1488 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
1489 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
1490 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
1491 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
1492 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
1493 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
1494 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
1495 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
1496 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
1497 default:
1498 assert(!"Invalid opcode provided");
1499 }
1500 return 0;
1501}
1502
1503CastInst *CastInst::create(Instruction::CastOps op, Value *S, const Type *Ty,
1504 const std::string &Name, BasicBlock *InsertAtEnd) {
1505 // Construct and return the appropriate CastInst subclass
1506 switch (op) {
1507 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
1508 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
1509 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
1510 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
1511 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
1512 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
1513 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
1514 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
1515 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
1516 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
1517 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
1518 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
1519 default:
1520 assert(!"Invalid opcode provided");
1521 }
1522 return 0;
1523}
1524
Reid Spencer5c140882006-12-04 20:17:56 +00001525CastInst *CastInst::createZExtOrBitCast(Value *S, const Type *Ty,
1526 const std::string &Name,
1527 Instruction *InsertBefore) {
1528 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1529 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1530 return create(Instruction::ZExt, S, Ty, Name, InsertBefore);
1531}
1532
1533CastInst *CastInst::createZExtOrBitCast(Value *S, const Type *Ty,
1534 const std::string &Name,
1535 BasicBlock *InsertAtEnd) {
1536 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1537 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1538 return create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
1539}
1540
1541CastInst *CastInst::createSExtOrBitCast(Value *S, const Type *Ty,
1542 const std::string &Name,
1543 Instruction *InsertBefore) {
1544 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1545 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1546 return create(Instruction::SExt, S, Ty, Name, InsertBefore);
1547}
1548
1549CastInst *CastInst::createSExtOrBitCast(Value *S, const Type *Ty,
1550 const std::string &Name,
1551 BasicBlock *InsertAtEnd) {
1552 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1553 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1554 return create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
1555}
1556
1557CastInst *CastInst::createTruncOrBitCast(Value *S, const Type *Ty,
1558 const std::string &Name,
1559 Instruction *InsertBefore) {
1560 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1561 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1562 return create(Instruction::Trunc, S, Ty, Name, InsertBefore);
1563}
1564
1565CastInst *CastInst::createTruncOrBitCast(Value *S, const Type *Ty,
1566 const std::string &Name,
1567 BasicBlock *InsertAtEnd) {
1568 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1569 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1570 return create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
1571}
1572
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001573CastInst *CastInst::createPointerCast(Value *S, const Type *Ty,
1574 const std::string &Name,
1575 BasicBlock *InsertAtEnd) {
1576 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001577 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001578 "Invalid cast");
1579
Chris Lattner03c49532007-01-15 02:27:26 +00001580 if (Ty->isInteger())
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001581 return create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
1582 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1583}
1584
1585/// @brief Create a BitCast or a PtrToInt cast instruction
1586CastInst *CastInst::createPointerCast(Value *S, const Type *Ty,
1587 const std::string &Name,
1588 Instruction *InsertBefore) {
1589 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001590 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001591 "Invalid cast");
1592
Chris Lattner03c49532007-01-15 02:27:26 +00001593 if (Ty->isInteger())
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001594 return create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
1595 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1596}
1597
Reid Spencer7e933472006-12-12 00:49:44 +00001598CastInst *CastInst::createIntegerCast(Value *C, const Type *Ty,
1599 bool isSigned, const std::string &Name,
1600 Instruction *InsertBefore) {
Chris Lattner03c49532007-01-15 02:27:26 +00001601 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer7e933472006-12-12 00:49:44 +00001602 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1603 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1604 Instruction::CastOps opcode =
1605 (SrcBits == DstBits ? Instruction::BitCast :
1606 (SrcBits > DstBits ? Instruction::Trunc :
1607 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1608 return create(opcode, C, Ty, Name, InsertBefore);
1609}
1610
1611CastInst *CastInst::createIntegerCast(Value *C, const Type *Ty,
1612 bool isSigned, const std::string &Name,
1613 BasicBlock *InsertAtEnd) {
Chris Lattner03c49532007-01-15 02:27:26 +00001614 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer7e933472006-12-12 00:49:44 +00001615 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1616 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1617 Instruction::CastOps opcode =
1618 (SrcBits == DstBits ? Instruction::BitCast :
1619 (SrcBits > DstBits ? Instruction::Trunc :
1620 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1621 return create(opcode, C, Ty, Name, InsertAtEnd);
1622}
1623
1624CastInst *CastInst::createFPCast(Value *C, const Type *Ty,
1625 const std::string &Name,
1626 Instruction *InsertBefore) {
1627 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1628 "Invalid cast");
1629 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1630 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1631 Instruction::CastOps opcode =
1632 (SrcBits == DstBits ? Instruction::BitCast :
1633 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
1634 return create(opcode, C, Ty, Name, InsertBefore);
1635}
1636
1637CastInst *CastInst::createFPCast(Value *C, const Type *Ty,
1638 const std::string &Name,
1639 BasicBlock *InsertAtEnd) {
1640 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1641 "Invalid cast");
1642 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1643 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1644 Instruction::CastOps opcode =
1645 (SrcBits == DstBits ? Instruction::BitCast :
1646 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
1647 return create(opcode, C, Ty, Name, InsertAtEnd);
1648}
1649
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001650// Provide a way to get a "cast" where the cast opcode is inferred from the
1651// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001652// logic in the castIsValid function below. This axiom should hold:
1653// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
1654// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001655// casting opcode for the arguments passed to it.
1656Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00001657CastInst::getCastOpcode(
1658 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001659 // Get the bit sizes, we'll need these
1660 const Type *SrcTy = Src->getType();
1661 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr/packed
1662 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/packed
1663
1664 // Run through the possibilities ...
Chris Lattner03c49532007-01-15 02:27:26 +00001665 if (DestTy->isInteger()) { // Casting to integral
1666 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001667 if (DestBits < SrcBits)
1668 return Trunc; // int -> smaller int
1669 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00001670 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001671 return SExt; // signed -> SEXT
1672 else
1673 return ZExt; // unsigned -> ZEXT
1674 } else {
1675 return BitCast; // Same size, No-op cast
1676 }
1677 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00001678 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001679 return FPToSI; // FP -> sint
1680 else
1681 return FPToUI; // FP -> uint
1682 } else if (const PackedType *PTy = dyn_cast<PackedType>(SrcTy)) {
1683 assert(DestBits == PTy->getBitWidth() &&
1684 "Casting packed to integer of different width");
1685 return BitCast; // Same size, no-op cast
1686 } else {
1687 assert(isa<PointerType>(SrcTy) &&
1688 "Casting from a value that is not first-class type");
1689 return PtrToInt; // ptr -> int
1690 }
1691 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
Chris Lattner03c49532007-01-15 02:27:26 +00001692 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00001693 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001694 return SIToFP; // sint -> FP
1695 else
1696 return UIToFP; // uint -> FP
1697 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
1698 if (DestBits < SrcBits) {
1699 return FPTrunc; // FP -> smaller FP
1700 } else if (DestBits > SrcBits) {
1701 return FPExt; // FP -> larger FP
1702 } else {
1703 return BitCast; // same size, no-op cast
1704 }
1705 } else if (const PackedType *PTy = dyn_cast<PackedType>(SrcTy)) {
1706 assert(DestBits == PTy->getBitWidth() &&
1707 "Casting packed to floating point of different width");
1708 return BitCast; // same size, no-op cast
1709 } else {
1710 assert(0 && "Casting pointer or non-first class to float");
1711 }
1712 } else if (const PackedType *DestPTy = dyn_cast<PackedType>(DestTy)) {
1713 if (const PackedType *SrcPTy = dyn_cast<PackedType>(SrcTy)) {
1714 assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
1715 "Casting packed to packed of different widths");
1716 return BitCast; // packed -> packed
1717 } else if (DestPTy->getBitWidth() == SrcBits) {
1718 return BitCast; // float/int -> packed
1719 } else {
1720 assert(!"Illegal cast to packed (wrong type or size)");
1721 }
1722 } else if (isa<PointerType>(DestTy)) {
1723 if (isa<PointerType>(SrcTy)) {
1724 return BitCast; // ptr -> ptr
Chris Lattner03c49532007-01-15 02:27:26 +00001725 } else if (SrcTy->isInteger()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001726 return IntToPtr; // int -> ptr
1727 } else {
1728 assert(!"Casting pointer to other than pointer or int");
1729 }
1730 } else {
1731 assert(!"Casting to type that is not first-class");
1732 }
1733
1734 // If we fall through to here we probably hit an assertion cast above
1735 // and assertions are not turned on. Anything we return is an error, so
1736 // BitCast is as good a choice as any.
1737 return BitCast;
1738}
1739
1740//===----------------------------------------------------------------------===//
1741// CastInst SubClass Constructors
1742//===----------------------------------------------------------------------===//
1743
1744/// Check that the construction parameters for a CastInst are correct. This
1745/// could be broken out into the separate constructors but it is useful to have
1746/// it in one place and to eliminate the redundant code for getting the sizes
1747/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001748bool
1749CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001750
1751 // Check for type sanity on the arguments
1752 const Type *SrcTy = S->getType();
1753 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType())
1754 return false;
1755
1756 // Get the size of the types in bits, we'll need this later
1757 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
1758 unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
1759
1760 // Switch on the opcode provided
1761 switch (op) {
1762 default: return false; // This is an input error
1763 case Instruction::Trunc:
Chris Lattner03c49532007-01-15 02:27:26 +00001764 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001765 case Instruction::ZExt:
Chris Lattner03c49532007-01-15 02:27:26 +00001766 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001767 case Instruction::SExt:
Chris Lattner03c49532007-01-15 02:27:26 +00001768 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001769 case Instruction::FPTrunc:
1770 return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() &&
1771 SrcBitSize > DstBitSize;
1772 case Instruction::FPExt:
1773 return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() &&
1774 SrcBitSize < DstBitSize;
1775 case Instruction::UIToFP:
Chris Lattner03c49532007-01-15 02:27:26 +00001776 return SrcTy->isInteger() && DstTy->isFloatingPoint();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001777 case Instruction::SIToFP:
Chris Lattner03c49532007-01-15 02:27:26 +00001778 return SrcTy->isInteger() && DstTy->isFloatingPoint();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001779 case Instruction::FPToUI:
Chris Lattner03c49532007-01-15 02:27:26 +00001780 return SrcTy->isFloatingPoint() && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001781 case Instruction::FPToSI:
Chris Lattner03c49532007-01-15 02:27:26 +00001782 return SrcTy->isFloatingPoint() && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001783 case Instruction::PtrToInt:
Chris Lattner03c49532007-01-15 02:27:26 +00001784 return isa<PointerType>(SrcTy) && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001785 case Instruction::IntToPtr:
Chris Lattner03c49532007-01-15 02:27:26 +00001786 return SrcTy->isInteger() && isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001787 case Instruction::BitCast:
1788 // BitCast implies a no-op cast of type only. No bits change.
1789 // However, you can't cast pointers to anything but pointers.
1790 if (isa<PointerType>(SrcTy) != isa<PointerType>(DstTy))
1791 return false;
1792
1793 // Now we know we're not dealing with a pointer/non-poiner mismatch. In all
1794 // these cases, the cast is okay if the source and destination bit widths
1795 // are identical.
1796 return SrcBitSize == DstBitSize;
1797 }
1798}
1799
1800TruncInst::TruncInst(
1801 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1802) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001803 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001804}
1805
1806TruncInst::TruncInst(
1807 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1808) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001809 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001810}
1811
1812ZExtInst::ZExtInst(
1813 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1814) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001815 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001816}
1817
1818ZExtInst::ZExtInst(
1819 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1820) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001821 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001822}
1823SExtInst::SExtInst(
1824 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1825) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001826 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001827}
1828
Jeff Cohencc08c832006-12-02 02:22:01 +00001829SExtInst::SExtInst(
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001830 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1831) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001832 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001833}
1834
1835FPTruncInst::FPTruncInst(
1836 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1837) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001838 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001839}
1840
1841FPTruncInst::FPTruncInst(
1842 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1843) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001844 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001845}
1846
1847FPExtInst::FPExtInst(
1848 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1849) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001850 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001851}
1852
1853FPExtInst::FPExtInst(
1854 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1855) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001856 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001857}
1858
1859UIToFPInst::UIToFPInst(
1860 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1861) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001862 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001863}
1864
1865UIToFPInst::UIToFPInst(
1866 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1867) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001868 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001869}
1870
1871SIToFPInst::SIToFPInst(
1872 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1873) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001874 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001875}
1876
1877SIToFPInst::SIToFPInst(
1878 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1879) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001880 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001881}
1882
1883FPToUIInst::FPToUIInst(
1884 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1885) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001886 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001887}
1888
1889FPToUIInst::FPToUIInst(
1890 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1891) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001892 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001893}
1894
1895FPToSIInst::FPToSIInst(
1896 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1897) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001898 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001899}
1900
1901FPToSIInst::FPToSIInst(
1902 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1903) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001904 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001905}
1906
1907PtrToIntInst::PtrToIntInst(
1908 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1909) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001910 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001911}
1912
1913PtrToIntInst::PtrToIntInst(
1914 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1915) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001916 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001917}
1918
1919IntToPtrInst::IntToPtrInst(
1920 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1921) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001922 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001923}
1924
1925IntToPtrInst::IntToPtrInst(
1926 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1927) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001928 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001929}
1930
1931BitCastInst::BitCastInst(
1932 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1933) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001934 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001935}
1936
1937BitCastInst::BitCastInst(
1938 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1939) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001940 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001941}
Chris Lattnerf16dc002006-09-17 19:29:56 +00001942
1943//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00001944// CmpInst Classes
1945//===----------------------------------------------------------------------===//
1946
1947CmpInst::CmpInst(OtherOps op, unsigned short predicate, Value *LHS, Value *RHS,
1948 const std::string &Name, Instruction *InsertBefore)
Reid Spencer542964f2007-01-11 18:21:29 +00001949 : Instruction(Type::Int1Ty, op, Ops, 2, Name, InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00001950 Ops[0].init(LHS, this);
1951 Ops[1].init(RHS, this);
1952 SubclassData = predicate;
1953 if (op == Instruction::ICmp) {
1954 assert(predicate >= ICmpInst::FIRST_ICMP_PREDICATE &&
1955 predicate <= ICmpInst::LAST_ICMP_PREDICATE &&
1956 "Invalid ICmp predicate value");
1957 const Type* Op0Ty = getOperand(0)->getType();
1958 const Type* Op1Ty = getOperand(1)->getType();
1959 assert(Op0Ty == Op1Ty &&
1960 "Both operands to ICmp instruction are not of the same type!");
1961 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00001962 assert((Op0Ty->isInteger() || isa<PointerType>(Op0Ty)) &&
Reid Spencerd9436b62006-11-20 01:22:35 +00001963 "Invalid operand types for ICmp instruction");
1964 return;
1965 }
1966 assert(op == Instruction::FCmp && "Invalid CmpInst opcode");
1967 assert(predicate <= FCmpInst::LAST_FCMP_PREDICATE &&
1968 "Invalid FCmp predicate value");
1969 const Type* Op0Ty = getOperand(0)->getType();
1970 const Type* Op1Ty = getOperand(1)->getType();
1971 assert(Op0Ty == Op1Ty &&
1972 "Both operands to FCmp instruction are not of the same type!");
1973 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00001974 assert(Op0Ty->isFloatingPoint() &&
Reid Spencerd9436b62006-11-20 01:22:35 +00001975 "Invalid operand types for FCmp instruction");
1976}
1977
1978CmpInst::CmpInst(OtherOps op, unsigned short predicate, Value *LHS, Value *RHS,
1979 const std::string &Name, BasicBlock *InsertAtEnd)
Reid Spencer542964f2007-01-11 18:21:29 +00001980 : Instruction(Type::Int1Ty, op, Ops, 2, Name, InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00001981 Ops[0].init(LHS, this);
1982 Ops[1].init(RHS, this);
1983 SubclassData = predicate;
1984 if (op == Instruction::ICmp) {
1985 assert(predicate >= ICmpInst::FIRST_ICMP_PREDICATE &&
1986 predicate <= ICmpInst::LAST_ICMP_PREDICATE &&
1987 "Invalid ICmp predicate value");
1988
1989 const Type* Op0Ty = getOperand(0)->getType();
1990 const Type* Op1Ty = getOperand(1)->getType();
1991 assert(Op0Ty == Op1Ty &&
1992 "Both operands to ICmp instruction are not of the same type!");
1993 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00001994 assert(Op0Ty->isInteger() || isa<PointerType>(Op0Ty) &&
Reid Spencerd9436b62006-11-20 01:22:35 +00001995 "Invalid operand types for ICmp instruction");
1996 return;
1997 }
1998 assert(op == Instruction::FCmp && "Invalid CmpInst opcode");
1999 assert(predicate <= FCmpInst::LAST_FCMP_PREDICATE &&
2000 "Invalid FCmp predicate value");
2001 const Type* Op0Ty = getOperand(0)->getType();
2002 const Type* Op1Ty = getOperand(1)->getType();
2003 assert(Op0Ty == Op1Ty &&
2004 "Both operands to FCmp instruction are not of the same type!");
2005 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00002006 assert(Op0Ty->isFloatingPoint() &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002007 "Invalid operand types for FCmp instruction");
2008}
2009
2010CmpInst *
2011CmpInst::create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
2012 const std::string &Name, Instruction *InsertBefore) {
2013 if (Op == Instruction::ICmp) {
2014 return new ICmpInst(ICmpInst::Predicate(predicate), S1, S2, Name,
2015 InsertBefore);
2016 }
2017 return new FCmpInst(FCmpInst::Predicate(predicate), S1, S2, Name,
2018 InsertBefore);
2019}
2020
2021CmpInst *
2022CmpInst::create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
2023 const std::string &Name, BasicBlock *InsertAtEnd) {
2024 if (Op == Instruction::ICmp) {
2025 return new ICmpInst(ICmpInst::Predicate(predicate), S1, S2, Name,
2026 InsertAtEnd);
2027 }
2028 return new FCmpInst(FCmpInst::Predicate(predicate), S1, S2, Name,
2029 InsertAtEnd);
2030}
2031
2032void CmpInst::swapOperands() {
2033 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2034 IC->swapOperands();
2035 else
2036 cast<FCmpInst>(this)->swapOperands();
2037}
2038
2039bool CmpInst::isCommutative() {
2040 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2041 return IC->isCommutative();
2042 return cast<FCmpInst>(this)->isCommutative();
2043}
2044
2045bool CmpInst::isEquality() {
2046 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2047 return IC->isEquality();
2048 return cast<FCmpInst>(this)->isEquality();
2049}
2050
2051
2052ICmpInst::Predicate ICmpInst::getInversePredicate(Predicate pred) {
2053 switch (pred) {
2054 default:
2055 assert(!"Unknown icmp predicate!");
2056 case ICMP_EQ: return ICMP_NE;
2057 case ICMP_NE: return ICMP_EQ;
2058 case ICMP_UGT: return ICMP_ULE;
2059 case ICMP_ULT: return ICMP_UGE;
2060 case ICMP_UGE: return ICMP_ULT;
2061 case ICMP_ULE: return ICMP_UGT;
2062 case ICMP_SGT: return ICMP_SLE;
2063 case ICMP_SLT: return ICMP_SGE;
2064 case ICMP_SGE: return ICMP_SLT;
2065 case ICMP_SLE: return ICMP_SGT;
2066 }
2067}
2068
2069ICmpInst::Predicate ICmpInst::getSwappedPredicate(Predicate pred) {
2070 switch (pred) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002071 default: assert(! "Unknown icmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002072 case ICMP_EQ: case ICMP_NE:
2073 return pred;
2074 case ICMP_SGT: return ICMP_SLT;
2075 case ICMP_SLT: return ICMP_SGT;
2076 case ICMP_SGE: return ICMP_SLE;
2077 case ICMP_SLE: return ICMP_SGE;
2078 case ICMP_UGT: return ICMP_ULT;
2079 case ICMP_ULT: return ICMP_UGT;
2080 case ICMP_UGE: return ICMP_ULE;
2081 case ICMP_ULE: return ICMP_UGE;
2082 }
2083}
2084
Reid Spencer266e42b2006-12-23 06:05:41 +00002085ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2086 switch (pred) {
2087 default: assert(! "Unknown icmp predicate!");
2088 case ICMP_EQ: case ICMP_NE:
2089 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2090 return pred;
2091 case ICMP_UGT: return ICMP_SGT;
2092 case ICMP_ULT: return ICMP_SLT;
2093 case ICMP_UGE: return ICMP_SGE;
2094 case ICMP_ULE: return ICMP_SLE;
2095 }
2096}
2097
2098bool ICmpInst::isSignedPredicate(Predicate pred) {
2099 switch (pred) {
2100 default: assert(! "Unknown icmp predicate!");
2101 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2102 return true;
2103 case ICMP_EQ: case ICMP_NE: case ICMP_UGT: case ICMP_ULT:
2104 case ICMP_UGE: case ICMP_ULE:
2105 return false;
2106 }
2107}
2108
Reid Spencerd9436b62006-11-20 01:22:35 +00002109FCmpInst::Predicate FCmpInst::getInversePredicate(Predicate pred) {
2110 switch (pred) {
2111 default:
2112 assert(!"Unknown icmp predicate!");
2113 case FCMP_OEQ: return FCMP_UNE;
2114 case FCMP_ONE: return FCMP_UEQ;
2115 case FCMP_OGT: return FCMP_ULE;
2116 case FCMP_OLT: return FCMP_UGE;
2117 case FCMP_OGE: return FCMP_ULT;
2118 case FCMP_OLE: return FCMP_UGT;
2119 case FCMP_UEQ: return FCMP_ONE;
2120 case FCMP_UNE: return FCMP_OEQ;
2121 case FCMP_UGT: return FCMP_OLE;
2122 case FCMP_ULT: return FCMP_OGE;
2123 case FCMP_UGE: return FCMP_OLT;
2124 case FCMP_ULE: return FCMP_OGT;
2125 case FCMP_ORD: return FCMP_UNO;
2126 case FCMP_UNO: return FCMP_ORD;
2127 case FCMP_TRUE: return FCMP_FALSE;
2128 case FCMP_FALSE: return FCMP_TRUE;
2129 }
2130}
2131
2132FCmpInst::Predicate FCmpInst::getSwappedPredicate(Predicate pred) {
2133 switch (pred) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002134 default: assert(!"Unknown fcmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002135 case FCMP_FALSE: case FCMP_TRUE:
2136 case FCMP_OEQ: case FCMP_ONE:
2137 case FCMP_UEQ: case FCMP_UNE:
2138 case FCMP_ORD: case FCMP_UNO:
2139 return pred;
2140 case FCMP_OGT: return FCMP_OLT;
2141 case FCMP_OLT: return FCMP_OGT;
2142 case FCMP_OGE: return FCMP_OLE;
2143 case FCMP_OLE: return FCMP_OGE;
2144 case FCMP_UGT: return FCMP_ULT;
2145 case FCMP_ULT: return FCMP_UGT;
2146 case FCMP_UGE: return FCMP_ULE;
2147 case FCMP_ULE: return FCMP_UGE;
2148 }
2149}
2150
Reid Spencer266e42b2006-12-23 06:05:41 +00002151bool CmpInst::isUnsigned(unsigned short predicate) {
2152 switch (predicate) {
2153 default: return false;
2154 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2155 case ICmpInst::ICMP_UGE: return true;
2156 }
2157}
2158
2159bool CmpInst::isSigned(unsigned short predicate){
2160 switch (predicate) {
2161 default: return false;
2162 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2163 case ICmpInst::ICMP_SGE: return true;
2164 }
2165}
2166
2167bool CmpInst::isOrdered(unsigned short predicate) {
2168 switch (predicate) {
2169 default: return false;
2170 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2171 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2172 case FCmpInst::FCMP_ORD: return true;
2173 }
2174}
2175
2176bool CmpInst::isUnordered(unsigned short predicate) {
2177 switch (predicate) {
2178 default: return false;
2179 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2180 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2181 case FCmpInst::FCMP_UNO: return true;
2182 }
2183}
2184
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002185//===----------------------------------------------------------------------===//
2186// SwitchInst Implementation
2187//===----------------------------------------------------------------------===//
2188
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002189void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002190 assert(Value && Default);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002191 ReservedSpace = 2+NumCases*2;
2192 NumOperands = 2;
2193 OperandList = new Use[ReservedSpace];
2194
2195 OperandList[0].init(Value, this);
2196 OperandList[1].init(Default, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002197}
2198
Misha Brukmanb1c93172005-04-21 23:48:37 +00002199SwitchInst::SwitchInst(const SwitchInst &SI)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002200 : TerminatorInst(Instruction::Switch, new Use[SI.getNumOperands()],
2201 SI.getNumOperands()) {
2202 Use *OL = OperandList, *InOL = SI.OperandList;
2203 for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
2204 OL[i].init(InOL[i], this);
2205 OL[i+1].init(InOL[i+1], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002206 }
2207}
2208
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002209SwitchInst::~SwitchInst() {
2210 delete [] OperandList;
2211}
2212
2213
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002214/// addCase - Add an entry to the switch instruction...
2215///
Chris Lattner47ac1872005-02-24 05:32:09 +00002216void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002217 unsigned OpNo = NumOperands;
2218 if (OpNo+2 > ReservedSpace)
2219 resizeOperands(0); // Get more space!
2220 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002221 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002222 NumOperands = OpNo+2;
2223 OperandList[OpNo].init(OnVal, this);
2224 OperandList[OpNo+1].init(Dest, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002225}
2226
2227/// removeCase - This method removes the specified successor from the switch
2228/// instruction. Note that this cannot be used to remove the default
2229/// destination (successor #0).
2230///
2231void SwitchInst::removeCase(unsigned idx) {
2232 assert(idx != 0 && "Cannot remove the default case!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002233 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
2234
2235 unsigned NumOps = getNumOperands();
2236 Use *OL = OperandList;
2237
2238 // Move everything after this operand down.
2239 //
2240 // FIXME: we could just swap with the end of the list, then erase. However,
2241 // client might not expect this to happen. The code as it is thrashes the
2242 // use/def lists, which is kinda lame.
2243 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
2244 OL[i-2] = OL[i];
2245 OL[i-2+1] = OL[i+1];
2246 }
2247
2248 // Nuke the last value.
2249 OL[NumOps-2].set(0);
2250 OL[NumOps-2+1].set(0);
2251 NumOperands = NumOps-2;
2252}
2253
2254/// resizeOperands - resize operands - This adjusts the length of the operands
2255/// list according to the following behavior:
2256/// 1. If NumOps == 0, grow the operand list in response to a push_back style
2257/// of operation. This grows the number of ops by 1.5 times.
2258/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
2259/// 3. If NumOps == NumOperands, trim the reserved space.
2260///
2261void SwitchInst::resizeOperands(unsigned NumOps) {
2262 if (NumOps == 0) {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002263 NumOps = getNumOperands()/2*6;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002264 } else if (NumOps*2 > NumOperands) {
2265 // No resize needed.
2266 if (ReservedSpace >= NumOps) return;
2267 } else if (NumOps == NumOperands) {
2268 if (ReservedSpace == NumOps) return;
2269 } else {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002270 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002271 }
2272
2273 ReservedSpace = NumOps;
2274 Use *NewOps = new Use[NumOps];
2275 Use *OldOps = OperandList;
2276 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2277 NewOps[i].init(OldOps[i], this);
2278 OldOps[i].set(0);
2279 }
2280 delete [] OldOps;
2281 OperandList = NewOps;
2282}
2283
2284
2285BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
2286 return getSuccessor(idx);
2287}
2288unsigned SwitchInst::getNumSuccessorsV() const {
2289 return getNumSuccessors();
2290}
2291void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
2292 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002293}
Chris Lattnerf22be932004-10-15 23:52:53 +00002294
2295
2296// Define these methods here so vtables don't get emitted into every translation
2297// unit that uses these classes.
2298
2299GetElementPtrInst *GetElementPtrInst::clone() const {
2300 return new GetElementPtrInst(*this);
2301}
2302
2303BinaryOperator *BinaryOperator::clone() const {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002304 return create(getOpcode(), Ops[0], Ops[1]);
Chris Lattnerf22be932004-10-15 23:52:53 +00002305}
2306
Reid Spencerd9436b62006-11-20 01:22:35 +00002307CmpInst* CmpInst::clone() const {
Reid Spencerfcb0dd32006-12-07 04:18:31 +00002308 return create(getOpcode(), getPredicate(), Ops[0], Ops[1]);
Reid Spencerd9436b62006-11-20 01:22:35 +00002309}
2310
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002311MallocInst *MallocInst::clone() const { return new MallocInst(*this); }
2312AllocaInst *AllocaInst::clone() const { return new AllocaInst(*this); }
2313FreeInst *FreeInst::clone() const { return new FreeInst(getOperand(0)); }
2314LoadInst *LoadInst::clone() const { return new LoadInst(*this); }
2315StoreInst *StoreInst::clone() const { return new StoreInst(*this); }
2316CastInst *TruncInst::clone() const { return new TruncInst(*this); }
2317CastInst *ZExtInst::clone() const { return new ZExtInst(*this); }
2318CastInst *SExtInst::clone() const { return new SExtInst(*this); }
2319CastInst *FPTruncInst::clone() const { return new FPTruncInst(*this); }
2320CastInst *FPExtInst::clone() const { return new FPExtInst(*this); }
2321CastInst *UIToFPInst::clone() const { return new UIToFPInst(*this); }
2322CastInst *SIToFPInst::clone() const { return new SIToFPInst(*this); }
2323CastInst *FPToUIInst::clone() const { return new FPToUIInst(*this); }
2324CastInst *FPToSIInst::clone() const { return new FPToSIInst(*this); }
2325CastInst *PtrToIntInst::clone() const { return new PtrToIntInst(*this); }
2326CastInst *IntToPtrInst::clone() const { return new IntToPtrInst(*this); }
2327CastInst *BitCastInst::clone() const { return new BitCastInst(*this); }
2328CallInst *CallInst::clone() const { return new CallInst(*this); }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002329SelectInst *SelectInst::clone() const { return new SelectInst(*this); }
2330VAArgInst *VAArgInst::clone() const { return new VAArgInst(*this); }
2331
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002332ExtractElementInst *ExtractElementInst::clone() const {
2333 return new ExtractElementInst(*this);
2334}
2335InsertElementInst *InsertElementInst::clone() const {
2336 return new InsertElementInst(*this);
2337}
2338ShuffleVectorInst *ShuffleVectorInst::clone() const {
2339 return new ShuffleVectorInst(*this);
2340}
Chris Lattnerf22be932004-10-15 23:52:53 +00002341PHINode *PHINode::clone() const { return new PHINode(*this); }
2342ReturnInst *ReturnInst::clone() const { return new ReturnInst(*this); }
2343BranchInst *BranchInst::clone() const { return new BranchInst(*this); }
2344SwitchInst *SwitchInst::clone() const { return new SwitchInst(*this); }
2345InvokeInst *InvokeInst::clone() const { return new InvokeInst(*this); }
2346UnwindInst *UnwindInst::clone() const { return new UnwindInst(); }
Chris Lattner5e0b9f22004-10-16 18:08:06 +00002347UnreachableInst *UnreachableInst::clone() const { return new UnreachableInst();}